This is my model:
UserModel = mongoose.Document & {
username: string,
password: string,
records: Record[]
};
Record: {
name: string;
date: Date;
}
Query:
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
});
});
This query returns all records, rather than records just from last 30 days. I am unable to locate where I am going wrong.
Edit: Even after using "lastDate.toISOString()" inplace of "lastDate" above, I still get all the results back.
Edit: Tested few other solution, like using "$filter", but still I get all records back.