Reputation: 1010
I have a date of joining field as doj (type : Date) in my MongoDB collection. I want to find the records of all employees who joined more than a month before from today. However it is giving any record where the doj is just even one day greater than the one month old date from today. I am not getting the one month older doj from today. Please help me with the mistakes that I am making.
I am using the following code :
var dt = new Date();
dt.setMonth(dt.getMonth() - 1);
collection.find({doj: {$gt:dt}}, function(error, docs) {
docs.forEach(function (doc) {
console.log("DOJ = "+ doc.doj);
}
});
Upvotes: 0
Views: 178
Reputation: 28179
If you want records with date of joining older then one month ago, you should query for {doj: {$lt:dt}}
Upvotes: 1