A.G.Progm.Enthusiast
A.G.Progm.Enthusiast

Reputation: 1010

How to find a record from MongoDB where the date field is more than a month older in Node JS

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

Answers (1)

marmor
marmor

Reputation: 28179

If you want records with date of joining older then one month ago, you should query for {doj: {$lt:dt}}

Upvotes: 1

Related Questions