I am trying to create a session_expiry time using Python and then inserting the session_expiry in mongo db using insert_one() method.
once the data in inserted in mongodb, while querying the mongo shell I am getting date in string format.
This is how it is getting stored -
"session_expiry" : "2023-06-23T19:55:41.551199"
but I want it to get stored in ISO format something like below date -
"session_expiry" : ISODate("2023-06-23T13:44:53.740Z")
import datetime
session_expiry = datetime.datetime.utcnow() + datetime.timedelta(minutes=90)
type of session_expiry is datetime.datetime`
`In [2]: session_expiry = datetime.datetime.utcnow() + datetime.timedelta(minutes=90)
In [3]: session_expiry
Out[3]: datetime.datetime(2023, 6, 23, 21, 55, 49, 2328)
In [4]: type(session_expiry)
Out[4]: datetime.datetime`
python:
import datetime
import pymongo
db = pymongo.MongoClient()['test']
session_expiry = datetime.datetime.utcnow() + datetime.timedelta(minutes=90)
db['mycollection'].insert_one({'session_expiry': session_expiry})
mongosh:
test> db.mycollection.findOne()
{
_id: ObjectId("64962241fa935efe824f5d07"),
session_expiry: ISODate("2023-06-24T00:22:49.372Z")
}