Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 678
codeofnode Avatar asked Oct 15 '25 16:10

codeofnode


1 Answers

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);
});
like image 54
JohnnyHK Avatar answered Oct 18 '25 10:10

JohnnyHK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!