简体   繁体   English

MongoDB 查询工作,但不适用于 Node.js Express Mongoose

[英]MongoDB query work but not with Node.js Express Mongoose

I have a date range query which tools several days and there were no answer.我有一个日期范围查询哪些工具几天没有答案。 i work with node.js express.js MongoDB Mongoose.我与 node.js express.js MongoDB Mongoose 合作。 This query is working perfectly in MongoDB:此查询在 MongoDB 中运行良好:

date =  new Date();
date.setDate(date.getDate() - 1); 
db.getCollection('c_crimes').find({DateTime:{$gte:date.toISOString(),$lte:new Date().toISOString()}})

but this query in my node code and with mongoose with the exact same database and other will return empty array []:但是这个查询在我的节点代码和 mongoose 与完全相同的数据库和其他将返回空数组 []:

 let dayDate = new Date();
 dayDate.setDate(dayDate.getDate() - 1);
 dayDate = dayDate.toISOString();
 await Crimes.find({DateTime: {$gte: dayDate,$lte: new Date().toISOString()}})

What should I do?我应该怎么办? Thanks.谢谢。

I found the problem.我发现了问题。 The problem was the data type.问题是数据类型。 in MongoDB, there was no problem with ranging the String date type but in Mongoose is very important to your data type be Date really and that's very important.在 MongoDB 中,确定字符串日期类型的范围没有问题,但在 Mongoose 中,对于您的数据类型非常重要,真的是日期,这非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM