如何使用mongoDB比较查询运算符检查createdAt日期之间的值? [英] How do I use the mongoDB Comparison Query Operator to check for values between createdAt dates?

查看:286
本文介绍了如何使用mongoDB比较查询运算符检查createdAt日期之间的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查看9月1日至9月30日之间在数据库中创建的所有已创建用户Meteor.users.find().fetch().如何在代码中实现这一目标.

I would like to view all the created users Meteor.users.find().fetch() that were created in the database between the beginning of 1st September and the end of 30th September. How do I achieve this in code.

找到内联我失败的尝试:

Find inline my failed attempt:

var beginningOfMonth = new Date();
beginningOfMonth.setMonth(8, 1); 
console.log("beginning Of the Month: "+beginningOfMonth); 

上面的代码是:

beginning Of the Month: Sat Sep 01 2018 21:44:00 GMT+0300 (East Africa Time)

...

var endOfMonth = new Date(); 
endOfMonth.setMonth(8, 30);     
console.log("end Of the Month: "+endOfMonth);

上面的代码产生:

end Of the Month: Sun Sep 30 2018 21:44:00 GMT+0300 (East Africa Time)

...到目前为止是完美的,但是在下面的查询中:

...perfect so far, however in the query below:

Meteor.users.find({ createdAt: { $gte: currentDate }}, { createdAt: { lte: endMonth }},  { "services.google.email": { $exists: true } } ).fetch();

上面的查询产生以下内容.请注意,createAt日期不在查询查询范围之内.

The query above yields the below. NOTE that the createAt dates lay outside what the query queries.

[{…}]
 0:
  createdAt: Thu Oct 04 2018 14:54:33 GMT+0300 (East Africa Time) 
  profile: {name: "Sir ProgrammerAllot"}
  services: {google: {…}, resume: {…}}
  _id: "2trR7WxnqKJuuipG8"
  __proto__: Object
 length: 1
  __proto__: Array(0)

如何修改查询以仅显示9月1日至9月最后一天之间在数据库中创建的用户?

How do I modify the query to only show the users created in the database between 1st of september and the last day of September?

期待您的回复.

推荐答案

您需要将所有查询词放入一个对象中(并使用$lte代替lte):

You need to put all your query terms into a single object (and use $lte instead of lte):

Meteor.users.find({
    createdAt: { $gte: currentDate, $lte: endMonth },
    "services.google.email": { $exists: true }
}).fetch();

这篇关于如何使用mongoDB比较查询运算符检查createdAt日期之间的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆