2

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.

Atul Kumar
  • 117
  • 1
  • 8
  • 1
    What if today is the 25th? Or the 31st? – Wernfried Domscheit Jul 19 '22 at 18:36
  • 1
    @WernfriedDomscheit, if today's date is any of 21 to 31 of a month, then filter should be such that when it gets applied on find function of mongoose, it fetches the record between 21 to 31st of Month of Recharge_Date. – Atul Kumar Jul 20 '22 at 06:44

1 Answers1

1

You can use moment.js or similar library:

db.collection.find({ 
  Recharge_Date: {
     $gte: moment().startOf('day').toDate(),
     $lte: moment().endOf('day').toDate()
  }
})
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110