简体   繁体   English

按月从mongodb文档中获取数据

[英]get data from mongodb document by month

I have a user document and have createdAt, so what should I do in the condition in order to get the data by month?我有一个用户文档并且已经创建了At,那么在条件下我应该怎么做才能按月获取数据?

the createdAt value look like this createdAt 值看起来像这样

2016-10-08T16:21:40.935Z
Account.find({'what to pass here?'}, function(err,response){
        if(!err){
            console.log(response)
        }
    });

Try putting this condition...试试把这个条件...

{"createdAt": {'$gte': new Date('2016-10-01'), '$lt': new Date('2016-10-31')}}

If you want these two dates to be generated dynamically using just a month number and year.如果您希望仅使用月份数字和年份动态生成这两个日期。 Put the below code.把下面的代码。

const month = 10;
const year = 2016;
const fromDate = new Date(year, month, 1);
const toDate = new Date(fromDate.getFullYear(), fromDate.getMonth() + 1, 0);

const condition = {"createdAt": {'$gte': date, '$lte': lastDay}};

Account.find(condition, function(err, response){
    if (!err){
        console.log(response);
    }
});

Try this试试这个

Account.find({
  "createdAt": {
    "$lte": "2016-10-08T16:21:40.935Z"
  }
}, function(err, response) {
  if (!err) {
    console.log(response)
  }
});

You can also define range to get specific data您还可以定义范围以获取特定数据

Account.find({
  "createdAt": {
     "$gte": "2016-10-06T16:21:40.935Z",
    "$lte": "2016-10-08T16:21:40.935Z"
  }
}, function(err, response) {
  if (!err) {
    console.log(response)
  }
});

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

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