Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an ISODate with pyMongo

I've been trying to find a way to create an ISODate object whith pyMongo client, but without any success so far.

I use http://pypi.python.org/pypi/pymongo3 client, which is the only serious one available in Python 3 for now, but the problem doesn't seem to come from this specific pymongo version.

I'd like to know if any of you has found a solution to use this MongoDB object type from a pymongo client... thanks for your help !

like image 210
Guillaume Lebourgeois Avatar asked Oct 04 '11 16:10

Guillaume Lebourgeois


People also ask

How do I use ISODate in Python?

To get an ISO 8601 date in string format in Python 3, you can simply use the isoformat function. It returns the date in the ISO 8601 format. For example, if you give it the date 31/12/2017, it'll give you the string '2017-12-31T00:00:00'.

What is ISODate in MongoDB?

You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 0 through 9999 to the new Date() constructor or the ISODate() function. These functions accept the following formats: new Date("<YYYY-mm-dd>") returns the ISODate with the specified date.

Is PyMongo an ODM?

PyMODM is a “core” ODM, meaning that it provides simple, extensible functionality that can be leveraged by other libraries to target platforms like Django. At the same time, PyMODM is powerful enough to be used for developing applications on its own.

Does PyMongo support async?

The only async framework that PyMongo fully supports is Gevent. Currently there is no great way to use PyMongo in conjunction with Tornado or Twisted. PyMongo provides built-in connection pooling, so some of the benefits of those frameworks can be achieved just by writing multi-threaded code that shares a Connection.


2 Answers

You just need to store an instance of datetime.datetime.

Inserting from the python shell:

>>> c.test.test.insert({'date': datetime.datetime.utcnow()}) ObjectId('4e8b388367d5bd2de0000000') >>> c.test.test.find_one() {u'date': datetime.datetime(2011, 10, 4, 16, 46, 59, 786000), u'_id': ObjectId('4e8b388367d5bd2de0000000')} 

Querying in the mongo shell:

> db.test.findOne() {     "_id" : ObjectId("4e8b388367d5bd2de0000000"),     "date" : ISODate("2011-10-04T16:46:59.786Z") } 
like image 105
Bernie Hackett Avatar answered Sep 17 '22 18:09

Bernie Hackett


For those who are wondering how to create ISODate from timestamp:

ts = time.time() isodate = datetime.datetime.fromtimestamp(ts, None) 

This will create datetime object with no timezone. When inserted to MongoDB it will get converted to proper ISODate().

Also, I strongly recommend looking at Python TimeTransitionsImage. Note that tuple here is named tuple (equivalent to struct in C). And also note that tuple fields are not the same as in C counterparts, even though the naming is the same (for instance, tm_wday starts with Monday and not Sunday).

like image 32
johndodo Avatar answered Sep 17 '22 18:09

johndodo