zoukankan      html  css  js  c++  java
  • mongdi db _id 转时间戳

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import json
    from pymongo import MongoClient
    import time
    import pymongo
    import time
    import datetime
    from bson.objectid import ObjectId
    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)
    
    myclient =MongoClient("mongodb://20.5.101.31:27017/")
    mydb = myclient["tlyy"]
    mycol = mydb["checkf5"]
    print myclient.list_database_names()
    print mydb.list_collection_names()
    x = mycol.find().sort([("_id",-1)]).limit(1)
    #print x.count()
    mydata= mycol.find()
    arr1=[]
    #for y in  mycol.find().sort([("_id",-1)]):
    for y in mycol.find().sort([("_id", -1)]).limit(1):
        print y['_id']
        print type(y['_id'])
        print id2time(str(y['_id']))
        arr1= y['f5dict']
        print arr1
    
    
    
    C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled/checkf5.py
    [u'local', u'tlyy']
    [u'checkf5', u'db_test', u'dbpro', u'esb', u'system.indexes']
    5cdeab8aa50a0b6015ca591b
    <class 'bson.objectid.ObjectId'>
    2019-05-17 20:39:38
    
    Process finished with exit code 0
    
    
  • 相关阅读:
    模拟测试20190806
    替罪羊树学习日记
    [Usaco2015 Jan]Moovie Mooving
    [NOIP2016]愤怒的小鸟
    [BZOJ1556]墓地秘密
    [SDOI2009]学校食堂Dining
    [SCOI2008]奖励关
    [洛谷3930]SAC E#1
    [BZOJ2809/APIO2012]dispatching
    [Usaco2018 Open]Disruption
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348768.html
Copyright © 2011-2022 走看看