MongoDB里的_id字段前四位是时间戳的16进制表示,通过Python可以很容易从_id中提取出时间戳来
def timestamp_from_objectid(objectid): result = 0 try: result = time.mktime(objectid.generation_time.timetuple()) except: pass return result
调用方法
print(timestamp_from_objectid(ObjectId('5217a543dd99a6d9e0f74702')))
返回:1377252547.0