如何在mongodb中使用$ gte和$ lte作为日期?

时间:2018-12-03 13:57:18

标签: mongodb sorting date meteor

我有一个集合中的数据。每个存储的数据都与指示每个数据插入集合的时间一起存储。

我在制定查询时遇到问题,该查询显示在特定月份的开始到特定月份的结束之间存储的数据。 请帮助我查询的问题所在。

在我的收藏集的内容下方找到。

Meteor.users.find({}).fetch(); 

(2) [{…}, {…}]
 0: {_id: "ZwT3hcmabytf82wyK", createdAt: Mon Dec 03 2018 11:16:53 GMT+0300 (East Africa Time)}
 1: {_id: "SEXZoHzt3ryKC2a4r", createdAt: Mon Dec 03 2018 11:16:34 GMT+0300 (East Africa Time)}
length: 2

请注意,createdAt建议插入数据的时间为:2018年12月3日,星期一,等。

现在在下面记下我的查询。 beginningOfMonth变量和endOfMonth变量都设置为 11月月(beginningOfMonth.setMonth(10, 1)endOfMonth.setMonth(10, 28)),但是查询结果是错误的:

var beginningOfMonth = new Date();
var endOfMonth = new Date();

beginningOfMonth.setMonth(10, 1);
endOfMonth.setMonth(10, 28);  

Meteor.users.find({}, { createdAt: { $gte: beginningOfMonth,  $lt: endOfMonth} }).fetch(); 

[{…}]
  0:{_id: "SEXZoHzt3ryKC2a4r", 
  createdAt: Mon Dec 03 2018 11:16:34 GMT+0300 (East Africa Time), 
  length: 1__proto__: Array(0)

请注意,查询结果是错误的。该查询似乎忽略了日期部分createdAt: { $gte: beginningOfMonth, $lt: endOfMonth}

请帮助我,告诉我我做错了什么。

预先感谢

1 个答案:

答案 0 :(得分:4)

  

告诉我我在做什么错

您没有正确使用流星API。将您的条件作为第一个参数。

Meteor.users.find({ createdAt: { $gte: beginningOfMonth,  $lt: endOfMonth} }).fetch(); 
相关问题