• mongodb 范围查找


    #!/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'])
    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, 07, 0, 0)
      end_timestamp = object_id_from_datetime(end_time)
      print end_timestamp
      range_search(start_timestamp, end_timestamp)
    
  • 相关阅读:
    execCommand快速实现复制到剪贴板
    webpack安装&指定版本安装&遇到的问题
    promise初体验
    使用原生JS发送AJAX请求(XML,JSON解析)
    【Python3】python中pymysql数据编码的问题
    闭包,闭包用途,call、apply、bind 的用法
    实现一个jQuery API
    Android 客户端和 web服务器通信
    Adb 基础
    在Android 中实现scp操作
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348764.html
Copyright © 2020-2023  润新知