Javascript Mangose嵌套发现不工作
这是我的模型:Javascript Mangose嵌套发现不工作,javascript,mongodb,mongoose,nodes,mean-stack,Javascript,Mongodb,Mongoose,Nodes,Mean Stack,这是我的模型: UserModel = mongoose.Document & { username: string, password: string, records: Record[] }; Record: { name: string; date: Date; } 查询: const date = new Date(); const lastDate = new Date(date.getTime() - (30 * 24
UserModel = mongoose.Document & {
username: string,
password: string,
records: Record[]
};
Record: {
name: string;
date: Date;
}
查询:
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
});
});
此查询返回所有记录,而不是仅从最近30天返回的记录。我找不到哪里出了问题
编辑:即使在使用“lastDate.toISOString()”代替上面的“lastDate”之后,我仍然可以得到所有结果
编辑:测试了一些其他的解决方案,比如使用“$filter”,但我仍然可以取回所有记录。您需要将日期转换为ISO格式,它以这种格式存储在DB中,然后它就可以工作。在完成此更改“{$gte”:lastDate.toISOString()}”之后,现在我没有取回任何记录。在保存日期时需要特别小心,以确保其有效?数据库中保存的日期格式是什么?其ISO日期格式是OK。。使用矩(lastDate).format()代替“.toISOString()”
.find()
查询不会“过滤”返回的数组内容。该任务属于检查日期
存储在记录
文档中的格式。在Mongo中,如果字段的筛选条件与格式不匹配,它将返回整个字段collection@Abhishek它以格式化日期的形式存储在文档中。如果您无法帮助,它很好。您不需要守夜。我试过你贴的任何复制品,但都不起作用。看我的编辑。