发布时间:2019-09-02 07:55:44编辑:auto阅读(1425)
ObjectID简介
mongo存储的数据在没有特别指定_id数据类型时,默认类型为ObjectID
‘_id’: ObjectId(‘55717c9eb2c983c127000000’)
ObjectId is a 12-byte BSON type, constructed using:
基本思路就是转换成时间对象 ,然后处理.
objectid – Tools for working with MongoDB ObjectIds
Tools for working with MongoDB ObjectIds.
class bson.objectid.ObjectId(oid=None)
Initialize a new ObjectId.
from bson.objectid import ObjectId
a = ObjectId('55717c9eb2c983c127000000')
a.generation_time.timetuple()
In [29]: a.generation_time.timetuple()
Out[29]: time.struct_time(tm_year=2015, tm_mon=6, tm_mday=5, tm_hour=10, tm_min=40, tm_sec=30, tm_wday=4, tm_yday=156, tm_isdst=0)
#到了这种格式就可以随便处理了
In [30]: time.strftime("%Y-%m-%d %H:%M:%S",a.generation_time.timetuple())
Out[30]: '2015-06-05 10:40:30'
# 时间对象转换
>>> gen_time = datetime.datetime(2010, 1, 1)
>>> dummy_id = ObjectId.from_datetime(gen_time)
>>> ObjectId(b'foo-bar-quux')
ObjectId('666f6f2d6261722d71757578')
>>> ObjectId('0123456789ab0123456789ab')
ObjectId('0123456789ab0123456789ab')
>>>
>>> # A u-prefixed unicode literal:
>>> ObjectId(u'0123456789ab0123456789ab')
ObjectId('0123456789ab0123456789ab')
上一篇: python爬虫之三:解析网络报文xml
下一篇: python动态加载模块、类、函数
47865
46431
37316
34764
29334
25994
24948
19969
19566
18053
5808°
6435°
5951°
5977°
7082°
5927°
5966°
6458°
6423°
7802°