说明:此处考虑了时区,mongodb默认使用 "格林威治时间"
1 #!/usr/bin/python3 2 3 import pymongo 4 import datetime 5 import pytz 6 7 #统计 8 """ 9 /usr/bin/pip3 install -Iv pymongo -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com; 10 /usr/bin/pip3 install -Iv pytz -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com; 11 """ 12 13 tzinfo = pytz.timezone('Asia/Shanghai') 14 15 client = pymongo.MongoClient( 16 host="x.x.x.x", 17 port=27017, 18 #username="root", 19 #password="123456", 20 #authSource="admin", # 在哪个数据库进行身份验证,默认是admin 21 tz_aware=True, # 设置为True 22 tzinfo=tzinfo # 加入时区信息 23 ) 24 db = client["db_name"] 25 collection = db["table_name"] 26 27 #此处正常的填写北京时间即可 28 #year, month=None, day=None, hour=0, minute=0, second=0,microsecond=0, tzinfo=None 2022, 9, 5, 3, 34, 27, 452000 29 dis_queue_all = { 30 "$and": [{"startTime": {"$gte": datetime.datetime(2022, 11, 4, 0, 0, 0, 0, tzinfo)}},{"startTime":{"$lt":datetime.datetime(2022, 11, 5, 0, 0, 0, 0, tzinfo)}}] 31 } 32 33 num = 0 34 for dis_all in collection.find(dis_queue_all): 35 num += 1 36 print("数量 ",num)
标签:bin,douban,mongodb,pypi,datetime,条数,表中,tzinfo,com From: https://www.cnblogs.com/fanrui/p/17405719.html