【发布时间】:2012-09-02 04:46:43
【问题描述】:
我正在将日期值添加到 MongoDB 集合中,作为 map-reduce 调用的一部分:
day = Date.UTC(this.time.getFullYear(), this.time.getMonth(), this.time.getDate());
emit({ user : this.user, day : day }, { count : 1 });
当我稍后在 Mongo shell 中查询这个集合时,我看到:
{ "_id" : { "user" : "assaf", "day" : 1331769600000 }, "value" : { "count" : 15 } }
{ "_id" : { "user" : "assaf", "day" : 1331856000000 }, "value" : { "count" : 57 } }
不知何故,日期看起来像一个整数——我猜它是某种时间戳表示。 如果我这样做:
PRIMARY> new Date(db.my_collection.find()[0]["_id"]["day"])
我找回了正确的日期:
ISODate("2012-03-19T00:00:00Z")
我的问题是如何在 pymongo 中做同样的事情。如果我对上述集合运行任何查询,pymongo 会返回其中 day 值作为浮点类型且与时间戳具有相同值的文档:
dict: {u'_id': {u'user': u'ariel', u'day': 1332115200000.0}, u'value': {u'count': 99.0}}
如何将此时间戳转换为 Python datetime?
【问题讨论】:
标签: python mongodb pymongo bson