#!/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