【问题标题】:How do I use the $gte and $lte for dates in mongodb?如何在 mongodb 中使用 $gte 和 $lte 作为日期?
【发布时间】:2019-05-04 20:09:00
【问题描述】:

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

我在制定查询时遇到了问题,该查询显示了在特定月份的月初和特定月份的月底之间存储的数据。 请帮助我解决我的查询有什么问题。

在我的收藏内容下方查找。

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}

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

提前致谢

【问题讨论】:

    标签: mongodb sorting date meteor


    【解决方案1】:

    告诉我我做错了什么。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-17
      • 2020-08-19
      • 2021-07-12
      • 2023-04-09
      • 2011-07-11
      • 2019-01-09
      • 2015-12-27
      • 1970-01-01
      相关资源
      最近更新 更多