Putting a Date object in MongoDB, returning a float when requested with pymongo

I am adding a date value to the MongoDB collection as part of a map reduction call:

day = Date.UTC(this.time.getFullYear(), this.time.getMonth(), this.time.getDate());
emit({ user : this.user, day : day }, { count : 1 });

When I later request this collection in the Mongo shell, I see:

{ "_id" : { "user" : "assaf", "day" : 1331769600000 }, "value" : { "count" : 15 } }
{ "_id" : { "user" : "assaf", "day" : 1331856000000 }, "value" : { "count" : 57 } }

Somehow the date looks like an integer - I think this is some representation of the timestamp. If I do this:

PRIMARY> new Date(db.my_collection.find()[0]["_id"]["day"])

I will return the correct date:

ISODate("2012-03-19T00:00:00Z")

My question is how to do the same in pimongo. If I run any query in the above collection, pymongo returns documents in which the value dayis of type float with the same value as the timestamp:

dict: {u'_id': {u'user': u'ariel', u'day': 1332115200000.0}, u'value': {u'count': 99.0}}

How to enable this timestamp in Python datetime?

+5
source share
2 answers

It looks like milliseconds from the era (January 1, 1970):

>>> from __future__ import division
>>> dict = {u'_id': {u'user': u'ariel', u'day': 1332115200000.0}, u'value': {u'count': 99.0}}
>>> datetime.datetime.utcfromtimestamp(dict['_id']['day'] / 1000.0)
datetime.datetime(2012, 3, 19, 0, 0)
>>>

UPDATE: ​​ .

+6

- , .

Date.UTC() , . , mongoDB . Date(), javascript . python . , , .

0

All Articles