Time conversion in Python3

the date format stored in MongoDB is 2018-07-01T00:00:00Z

the environmental background of the problems and what methods you have tried

related codes

collection.find_one(
    {
        "regDate": 
        {
            "$gte":ISODate("2018-07-01T00:00:00Z"),"$lt":ISODate("2018-07-31T00:00:00Z")
        }
    }
)
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
NameError: name "ISODate" is not defined
Mar.30,2021

according to your error prompt, you can take a look at this solution

https://jira.mongodb.org/browse/PYTHON-704

ISODate is used on the mongo command line, and datetime is available in python.

from datetime import datetime

collection.find_one(
    {
        "regDate": {
            "$gte": datetime(2018, 6, 1), "$lt": datetime(2018, 7, 31)
        }
    }
)
Menu