用于查询最近1小时数据的Python脚本

2024-05-18 04:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

使用下面的脚本,我试图获取在最后一个小时添加的多个集合的文档;但它给了我零价值

有人可以看看下面的代码,并帮助我修复它吗

import pymongo
import sys
from datetime import datetime
from datetime import timedelta
from pymongo import MongoClient
# establish connectivity to Mongodb via ssl using pymongo module
#args = sys.argv

host = 'mongo-db-prd'
uname = 'superuser'
passwrd = 'Hayyo'
#print (args)
port = "27017"
print(uname)
print(passwrd)
uri = 'mongodb://' + uname + ":" + passwrd + "@" + host + ":" + port + '/?authSource=admin'

client = MongoClient(uri, ssl=True, ssl_ca_certs='./files/rds-combined-ca-bundle.pem')
# This will create hl7feeds docdb
print("connected client")
db = client.feeds  # This command will create a DB
print(client.list_database_names()) # This command will print list of DBs
print(client.list_database_names()) # This command will print list of DBs
mycol = db[ "feeds_100"]  # This command will create a collection in DB
docins=mycol.insert_one({"name" : "test"})  # This will insert a document in collection
dblist = client.list_database_names()
print(client.list_database_names())

# Lets create collections on docdb for all tenants
tlist1 = ["feeds_104","feeds_105","feeds_106"]

for each_val in tlist1:
   print (each_val)
   countvalue = db.getCollection('each_val').find({"row_created_date":{"$gt":datetime.utcnow() - timedelta(hours=1)}}).count();
   print (countvalue)

Tags: fromimportclientdbdatetimenamescreatethis