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
    
    
  • 相关阅读:
    sed处理文本文件
    多节点ssh免密匙登录
    nmon监控工具的使用
    PostgreSQL 磁盘使用大小监控
    PostgreSQL 锁监控
    PostgreSQL installations
    《卸甲笔记》-多表查询之二
    《卸甲笔记》-多表查询之一
    《卸甲笔记》-子查询
    《卸甲笔记》-分组统计查询
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348768.html
Copyright © 2011-2022 走看看