Meteor miniMongo查询日期对象不起作用

时间:2014-10-14 13:36:17

标签: mongodb meteor

我在miniMongo中有很多文档,其中dateCreated值的值在下面列出的日期之间。值存储为dateCreated: ISODate("2014-05-10T16:00:00Z")

但查询无法提取其中任何一个(返回空数组);

startDate = new Date( "Thu Jan 01 2014 08:00:00 GMT+0800 (HKT)" );

endDate = new Date( "Thu Jan 01 2015 08:00:00 GMT+0800 (HKT)" );

Tests.find({ dateCreated: {$gt: startDate, $lt: endDate} }).fetch();

我也尝试以编程方式创建查询,但它仍然无法正常工作。

dateCreatedRange = {$gt: startDate, $lt: endDate}

Tests.find({ dateCreated: dateCreatedRange }).fetch();

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

使用momentjs格式化日期。这是我在我的应用程序中对日期范围所做的事情

var date1 = moment(xdate1).format("YYYY-MM-DDTHH:mm:ssZ");
var date2 = moment(xdate2).format("YYYY-MM-DDTHH:mm:ssZ");

其中xdate1和xdate2来自日期选择器,格式为'yyyy-mm-dd'

date1 = new Date(date1);
date2 = new Date(date2);

然后用它来查询

Tests.find({ dateCreated: {$gt: date1, $lt: date2} }).fetch();