node.js 用猫鼬和日期查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17554943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Querying with mongoose and dates
提问by dysphoria
I'm trying to find out how to do an specific query with mongoose. I have in mongodb something like this:
我试图找出如何使用猫鼬进行特定查询。我在 mongodb 中有这样的东西:
{ "_id" : 1, "modificationDate" : ISODate("2013-06-26T18:57:30.012Z") }
{ "_id" : 2, "modificationDate" : ISODate("2013-06-26T18:57:35.012Z") }
I want to obtain all the objects where the difference between the actual date and modificationDate is greater than 5 days.
我想获取实际日期和修改日期之间的差异大于 5 天的所有对象。
回答by JohnnyHK
Calculate the 5-days-old cutoff time and then perform a findusing the $ltoperator and the calculated cutoff:
计算 5 天前的截止时间,然后find使用$lt运算符和计算出的截止时间执行 a :
var cutoff = new Date();
cutoff.setDate(cutoff.getDate()-5);
MyModel.find({modificationDate: {$lt: cutoff}}, function (err, docs) { ... });

