I am centralizing data on my Atlas Cluster from bare-metal servers running community MongoDB databases locally. However I have an issue with my ISODates()
attributes that are converted to "$date"
dictionaries, after which the sort() query still works but the "$lt" and "$gt" queries are failing.
Attribute in local db
"createdTime": ISODate("2023-01-20T11:20:50.268Z")
Attribute in the Atlas Cluster'db :
"createdTime" : { '$date': '2023-01-20T11:20:50.268Z' }
In my Atlas Cluster's collection, I am still able to sort on the time attribute :
db.MyCollection.find().sort({createdTime:-1}).map(x => x.createdTime)
[
{ '$date': '2023-01-20T11:20:50.268Z' },
{ '$date': '2023-01-20T11:20:26.587Z' },
{ '$date': '2023-01-20T11:20:04.108Z' },
]
But when I try to use the operators "$gt"
and "$lt"
then mongo doesn't know what to do :
db.MyCollection.countDocuments({ createdTime: { $lt: ISODate("2023-02-01T00:00:00.000Z")} })
0
// even with "$date" as a key
db.MyCollection.countDocuments({ "createdTime.$date": { $lt: ISODate("2023-02-01T00:00:00.000Z")}})
0
Eventually, I figured I could make it work with a Javascript function to convert the "$date" objects back to ISODate, but I want to be able to use the "$lt" and "$gt" in my Python API, relying on pymongo.
Pymongo code :
res = db["MyCollection"].count_documents({"createdTime": {"$lt": datetime.now() }})
# res = 0
Any idea how I could solve what I assume is a date formatting issue?