Atul Kumar
Atul Kumar

Reputation: 129

Apply filter on date in mongodb

I have a field named RechargeDate in mongo :

"Recharge_Date" : ISODate("2022-06-13T00:00:00.000Z")

Now, I need to apply filter on it based on today's date. If today's date is any of 1 to 10 of a month, then filter should be such that when it gets applied on find function of mongoose, it fetches the record between 1 to 10th of Month of Recharge_Date.

Similarly, if today's date is any of 11 to 20 of a month, then filter should be such that when it gets applied on find function of mongoose, it fetches the record between 11 to 20th of Month of Recharge_Date.

I want to know how the filter query would look like.

Upvotes: 2

Views: 214

Answers (1)

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59436

You can use moment.js or similar library:

db.collection.find({ 
  Recharge_Date: {
     $gte: moment().startOf('day').toDate(),
     $lte: moment().endOf('day').toDate()
  }
})

Upvotes: 1

Related Questions