#!/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)
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)
def range_search(start_timestamp, end_timestamp):
connection = MongoClient("mongodb://20.5.101.31:27017/")
with connection:
db = connection['tlyy']
table = db['checkf5']
a = table.find({'_id':{'$lt' : end_timestamp, '$gte' : start_timestamp}})
print a
print type(a)
print a.limit(1000)
b=a.limit(1000)
print type(b)
for x in b:
#print str(x['_id'])+'=>'+id2time(str(x['_id']))+'=>'+str(x['f5dict'])
print str(x['_id']) + '=>' + id2time(str(x['_id']))
if __name__ == '__main__':
##时间要减去8
#start time
start_time = datetime.datetime(2019, 4, 6, 06, 0, 0)
start_timestamp = object_id_from_datetime(start_time)
print start_timestamp
#end time
end_time = datetime.datetime(2019, 4, 6, 06, 05, 0)
end_timestamp = object_id_from_datetime(end_time)
print end_timestamp
range_search(start_timestamp, end_timestamp)
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/untitled/F5/rang.py
5ca840600000000000000000
5ca8418c0000000000000000
<pymongo.cursor.Cursor object at 0x02817F70>
<class 'pymongo.cursor.Cursor'>
<pymongo.cursor.Cursor object at 0x02817F70>
<class 'pymongo.cursor.Cursor'>
5ca84087a50a0b6015c7a09a=>2019-04-06 14:00:39
5ca840c1a50a0b6015c7a09d=>2019-04-06 14:01:37
5ca840fea50a0b6015c7a0a0=>2019-04-06 14:02:38
5ca84139a50a0b6015c7a0a3=>2019-04-06 14:03:37
5ca84177a50a0b6015c7a0a6=>2019-04-06 14:04:39
Process finished with exit code 0