Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting a Date object into MongoDB, getting back a float when querying with pymongo

I'm adding a date value into a MongoDB collection as part of a map-reduce call:

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

When I later query 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 guess it's some timestamp representation. If I do this:

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

I get back the correct date:

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

My question is how to do the same in pymongo. If I run any query on the above collection, pymongo returns documents in which the day value as a float type 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 do I turn this timestamp into a Python datetime?

like image 982
Assaf Lavie Avatar asked Sep 08 '12 16:09

Assaf Lavie


People also ask

How to prepare a Python date object to be inserted into MongoDB?

How to prepare a Python date object to be inserted into MongoDB? You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo.

What is the difference between Mongo date query and date query?

MongoDB date query returns the date either as string or date object, date query will return the current date as a string in the mongo shell. MongoDB date will return the current date as a date object, and mongo shell will wrap the date object with isolate helper in MongoDB.

What is the use of Mongo date in mongo shell?

MongoDB date will return the current date as a date object, mongo shell will wrap the date object with isolate helper in MongoDB. We can specify the particular date by passing the date as ISO-8601 date string, this string is used with the range as 0 to 9999 to the new date () function in MongoDB.

What is the default DATETIME type in MongoDB?

MongoDB has no special datetime object and it uses the JavaScript Date type, which is stored in BSON form. Internally, Date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970). The official BSON specification refers to the BSON Date type as the UTC datetime.


1 Answers

Looks like milliseconds since epoch (1 Jan 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: Added division check from first comment.

like image 127
Qiau Avatar answered Sep 24 '22 03:09

Qiau