0

I want to fetch record which are equal to some date.

I got many example of $gte, $lte, not for $eq

When I do like this (with $gte),

db.getCollection('apiPerformance').find({CreatedAt : {$gte : new ISODate('2017-05-30')}})

then it is returning records.

But I want get record equal to date ($eq)

db.getCollection('apiPerformance').find({CreatedAt : {$eq : new ISODate('2017-05-30')}})

Then It is not returning any data.

But when I filter with Date with Time like

db.getCollection('apiPerformance').find({CreatedAt : {$eq : new ISODate('2017-05-30 22:00:00.000Z')}})

then returning data.

Please guide me how to use $eq in MongoDb to match with only date.???

GOPAL SHARMA
  • 657
  • 2
  • 12
  • 37
  • It's a "range" and not an "equality": `.find({ "CreatedAt": { "$gte": new Date("2017/05/30") "$lt": new Date("2017/05/31") } })` between "today" and "tomorrow". Or the day you want and the "next" day. – Neil Lunn May 31 '17 at 10:00
  • isn't any direct way to solve this without giving range? All I want to use only $eq. – GOPAL SHARMA May 31 '17 at 10:11
  • What you are not understanding is when you say `new Date("2017/05/30")` then you are actually returning a specific timestamp value `1496066400000` which is in fact "exactly" what MongoDB is storing in all date objects. Now unless you happened to save every date as only the "date" value on that day and therefore having "exactly" that value, then an "equality" does not match. So the "date" is actually the "range" from the "start" of the day until the "end" of the day, or more accurately "less than" the start of the "next day". This is the way it has always been done. In every database. – Neil Lunn May 31 '17 at 10:16

0 Answers0