codeofnode
codeofnode

Reputation: 18609

how to query with mongoose timestamp date

I have used mongoose-timestamp plugin https://github.com/drudge/mongoose-timestamp

Which maintains createdAt and updatedAt fields on the documents

I want to find the documents which have been updated after certain Date, how should i do that??

This is what i am doing :

Model.find({updatedAt : { $gte : new Date(2014, 5, 24)} }, function(err, docs){
    console.log(docs); //prints empty arry []
});

However i have checked in mongoose console.. i find there are documents with updatedAt fields having value of after 24May2014.

What mistake i am doing?

Upvotes: 4

Views: 14817

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

The month parameter in the JavaScript Date constructor is 0-based, so you're querying for docs having an updatedAt value after June 24, 2014.

> new Date(2014, 5, 24)
Tue Jun 24 2014 00:00:00 GMT-0500 (Central Daylight Time)

So your query is fine otherwise and you just need to update it to use 4 for the month:

Model.find({updatedAt : { $gte : new Date(2014, 4, 24)} }, function(err, docs){
    console.log(docs);
});

Upvotes: 9

Related Questions