Here is the model I am working with:
UserModel = mongoose.Document & {
username: string,
password: string,
records: Record[]
};
Record: {
name: string;
date: Date;
}
My current query snippet is as follows:
const date = new Date();
const lastDate = new Date(date.getTime() - (30 * 24 * 60 * 60 * 1000));
UserModel.find({ "records" : { "$elemMatch": { "date" : { "$gte": lastDate } } }}, (err, userRecords: any) => {
if (err) {
return res.json({
"status": "error",
"detail": err
});
}
return res.json({
"records": userRecords
});
});
My issue is that the query is returning all records instead of just those from the last 30 days. I am struggling to identify the mistake in my approach.
Edit: I also tried using "lastDate.toISOString()" instead of "lastDate" but still getting all results.
Edit: I attempted using "$filter" and other solutions, but the problem persists with all records being returned.