Home > database >  Mongodb - Using a long number to filter a Date type field in aggregation
Mongodb - Using a long number to filter a Date type field in aggregation

Time:12-26

I'm trying to filter a collection on a date type field using a long number (number of ms since epoch) and can't seem to get it to work in a regular find() or an aggregation. What am I missing ?

TIA!

db.testColl.insertOne({_id:"test", createDate: ISODate("2022-12-26T01:00:00.000 0000")})
db.testColl.find( {createDate: {$gte : {$toDate:1608947820333} }} )

CodePudding user response:

I think you can't use aggregate expression $toDate in find/match. Instead, try using it with $expr.

db.testColl.find({
  $expr: {
    $gte: [
      "$createDate",
      {
        $toDate: 1608947820333
      }
    ]
  }
})
  • Related