I'm trying to write a date comparison query using MongoDB's strict JSON representation of BSON.
I'd like it to work in the MongoDB shell (v2.4.3)
Here's what I've tried...
Setup: create a new document with an at
date of Jan 1, 2020
> db.myTimes.insert({"at": new Date("2020-01-01")})
Using non-strict query for date > 2010, no problem:
> db.myTimes.find({"at": {"$gt": new Date("2010-01-01")}})
{ "_id" : ObjectId([snipped]), "at" : ISODate("2020-01-01T00:00:00Z") }
Using strict JSON query, however... NO DICE
> db.myTimes.find({"at": {"$gt": {"$date":"2010-01-01T00:00:00Z"}}})
> db.myTimes.find({"at": {"$gt": {"$date":"2010-01-01"}}})
> db.myTimes.find({"at": {"$gt": {"$date": 1262304000000}}})
> db.myTimes.find({"at": {"$lte": {"$date": 1262304000000}}})
(As you can see, I tried ISO8601 dates, epoch times, and also changing my $gt
to $lte
on the theory that these would be mutually exclusive, so one of them should return something :-)
Thanks for any pointers!
-B