Hello! I’m relatively new to MongoDB, and am having trouble understanding why my query is taking so long. To give a bit of background, I’m creating a query to pull down activity data for users, where for each user their activity is stored as an array of entries with times attached. I attempted to create a combined index for my DB to make userid and time queries quicker. It looks like this: userid_1_activity.time_1
in Atlas.
My query looks like this:
import connectMongoDB from "../../../../../../libs/mongoDB";
import Personal from "../../../../../../models/Personal";
export async function GET(request, { params }) {
await connectMongoDB();
const { id, date, timezone } = params;
const startOfDay = new Date(date);
startOfDay.setHours(timezone, 0, 0);
const endOfDay = new Date(new Date(date).setDate(startOfDay.getDate() + 1));
endOfDay.setHours(0, 0, 0);
const user = await Personal.aggregate([
{
$match: {
userid: id,
},
},
{
$unwind: "$activity",
},
{
$match: {
"activity.time": {
$gte: startOfDay,
$lt: endOfDay,
},
},
},
{
$project: {
_id: 0,
activity: 1,
},
},
]);
console.log(user.length);
const activity = user ? user.map((a) => a.activity) : [];
return Response.json({ response: activity }, { status: 200 });
}
``
If its of any help, Atlas is taking a ridiculously long amount of time to pull down the documents when I open the database. Also, there are 42000 datapoints in the document I'm querying.