简体   繁体   English

日期之间的Meteor.js / MongoDB查询不返回数据

[英]Meteor.js / MongoDB between dates query not returning data

I have the following piece of code for getting results back from a Mongo Collection. 我有以下代码用于从Mongo Collection中获取结果。

var currentDate = moment().toISOString();
// RETURNING: 2016-12-10T20:36:04.494Z

var futureDate = moment().add(10, "days").toISOString();
// RETURNING: 2016-12-20T20:36:04.495Z

return agenda = Agendas.find({
  "agendaDate": { '$gte': currentDate, '$lte': futureDate }
});

And the date is stored in MongoDB Collection like below; 日期存储在MongoDB Collection中,如下所示;

{ 
"_id" : ObjectId("584877e56466dd236cd95f15"), 
"agendaDate" : ISODate("2016-12-12T17:28:25.000+0000"), 
"agendaTime" : "20:59", 
"agendaEvent" : "Test event" 
}

However, I am not getting any results returning as all. 但是,我没有得到任何结果。 I have set up 3 test documents, 2 in the range, 1 outside. 我已经设置了3个测试文档,2个在范围内,1个在外面。

Can anyone explain what I'm doing wrong and help rectify the code? 任何人都可以解释我做错了什么并帮助纠正代码?

You need to compare dates against actual date objects, not strings representing them. 您需要将日期与实际日期对象进行比较,而不是表示它们的字符串。

That is, you need to get the date from your moment objects, using the toDate() method. 也就是说,您需要使用toDate()方法从您的moment对象中获取日期。

var futureDate = moment().add(10, "days").toDate();

Well actually moment.toISOString() returns a string, so you can't use it to compare with date object in your mongodb query. 实际上,moment.toISOString()返回一个字符串,因此您无法使用它与mongodb查询中的日期对象进行比较。 You should consider creating a date object for that. 您应该考虑为此创建日期对象。 Regs, Yann Regs,Yann

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

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