-1

I want a help in getting the data for the next 30 days. I have,

db.employee.find(
{
    "idExpiryDate": 
    {
        $gte: new Date()
    }
}
).sort({ "idExpiryDate": 1 })

In this I get all the idExpiryDate after today, but I want only the next 30 days data. Anyone can help?

Viveka
  • 175
  • 2
  • 12
  • Older than would subtract from the current date and use `$gte` or "greater than" and dates after the current would add to the current date and be "less than" or `$lte`. Probably just as `$gte` the "now" and `$lte` the future date unless you wanted older. You do the math before you send the query and not during or after. – Neil Lunn Oct 25 '19 at 12:41

2 Answers2

0

Try for the following:

db.employee.find(
{
    "idExpiryDate": 
    {
        $gte: new Date((new Date().getTime() - (30 * 24 * 60 * 60 * 1000)))
    }
}
).sort({ "idExpiryDate": -1 })
Subburaj
  • 5,114
  • 10
  • 44
  • 87
0

Use the less than ($lt) operator as well, adding 2,592,000,000 (number of milliseconds in 30 days) to the current date:

db.employee.find(

    "idExpiryDate": 
    {
        $gte: new Date(),
        $lt: new Date(new Date().valueOf() + 2952000000)
    }
}
).sort({ "idExpiryDate": 1 })
Joe
  • 25,000
  • 3
  • 22
  • 44