mongodb _id 转时间

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
from pymongo import MongoClient
import time
def id2time(object_id):
    timeStamp = int(object_id[:8], 16)
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))
import pymongo
import time
import datetime
from bson.objectid import ObjectId


def object_id_from_datetime(from_datetime=None):
    ''' According to the time manually generated an ObjectId '''
    if not from_datetime:
        from_datetime = datetime.datetime.now()
    return ObjectId.from_datetime(generation_time=from_datetime)
print id2time('5cdea9e7a50a0b6015ca5906')

 

上一篇:NodeJs操作MongoDB之多表查询($lookup)与常见问题


下一篇:2020MongoDB基本命令-R操作(查)