I am trying to filter date on MongoDB via pymongo. Specifically looking to "match" all dates greater than a given date. My query is definitely correct, as it queries perfectly in MongoDB compass - aggregations. However once I add this to my pymongo code it returns zero results. So as mentioned it did not work, so I have confirmed via the compass application, that my query string is infact correct, please assist, my code below is what I used to query in compass which works, and further below is my full aggregate pipeine in pymongo - which doesn't work. ( BTW: My date format is stored correctly in mongo - as "date")
{
// MongoDB compass code - works fine.
"date": {"$gt": new Date("2020-12-06T00:00:00.000+00:00")}
}
//Pymongo code
betdate = weekcol.aggregate([
## stage 1
{"$match":{
"date": {"$gt": new Date("2020-12-06T00:00:00.000+00:00")}
} }
])
for bdate in betdate:
print(bdate)
The above pymongo code, throws me the error:
File "", line 9 "date": {"$gt": new Date("2020-12-06T00:00:00.000+00:00")} SyntaxError: invalid syntax
I had a feeling it was the new date() which was a problem, but I changed it to ISODate(), and it still doesn't work.
The equivalent in pymongo is:
import datetime
import pytz
filter1 = {"date": {"$gt": datetime.datetime(2020, 12, 6, 0, 0, tzinfo=pytz.utc)}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.