Maria Jane Maria Jane - 2 months ago 11
Node.js Question

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?

the createdAt value look like this

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

Answer

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)
  }
});