My current dilemma involves extracting student data for each week within an academic year based on a start date and end date. To achieve this, I have devised an array that contains the start and end dates for every week. The process involves iterating through each week, querying the database in a loop like so:
let allWeeksData = []
let groupQuery: any = {
_id: {
attendance_code: "$attendance_code",
},
total: { $sum: 1 },
};
for(let dateRanges of dateRangesArray)
{
const startDate = dateRanges.start_date;
const endDate = dateRanges.end_date;
const rawResults = await sessionAttendanceModel.aggregate([
{
$match: {
student_school: { $in: studentSchoolIDs },
"date.date": {
$gte: new Date(startDate),
$lte: new Date(endDate),
},
attendance_code: {
$in: usedAttendanceCodes,
},
},
},
{
$group: groupQuery,
},
]);
rawResults.start_date = startDate
rawResults.end_date = endDate
allWeeksData.push(rawResults)
}
However, this method has proven to be inefficiently slow. Is there a more streamlined approach to execute a single aggregate group query on the database and achieve the same outcome?