ISODate originating from PyMongo insert

48 views
Skip to first unread message

Robert James Liguori

unread,
Jun 13, 2016, 8:51:12 AM6/13/16
to mongodb-user

In my JSON I have the following:

"data" : "2010-03-02T19:57:41.500Z"

I want to insert this date with PyMongo as an ISODate.

How do I do this?

Thanks,
Robert

Bernie Hackett

unread,
Jun 13, 2016, 9:49:01 PM6/13/16
to mongodb-user
The format of your date isn't quite right for MongoDB Extended JSON, but this is how:

>>> import pymongo
>>> from bson.json_util import loads
>>> client = pymongo.MongoClient()
>>> client.test.test.insert_one(loads('{"data" : {"$date": "2010-03-02T19:57:41.500Z"}}'))
<pymongo.results.InsertOneResult object at 0x7f0e3689ca00>
>>> client.test.test.find_one()
{u'_id': ObjectId('575f61adfa5bd80b59af282d'), u'data': datetime.datetime(2010, 3, 2, 19, 57, 41, 500000)}

MongoDB Enterprise > use test
switched to db test
MongoDB Enterprise > db.test.findOne()
{
"_id" : ObjectId("575f61adfa5bd80b59af282d"),
"data" : ISODate("2010-03-02T19:57:41.500Z")
}

See the MongoDB Extended JSON docs for $date: https://docs.mongodb.com/manual/reference/mongodb-extended-json/#date
Reply all
Reply to author
Forward
0 new messages