繁体   English   中英

用于返回最近 1 小时内在 MongoDB 集合中添加的记录的 Python 脚本

[英]Python Script to return records added in last 1 hour in MongoDB collection

上面的脚本在 gt 处给出错误为无效语法; 有人可以帮助我吗? 我试图检索过去 1 小时内插入的记录。

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)```

您已经在python使用了javascript代码:

countvalue = db.getCollection('each_val').find({"row_created_date":{"$gt":new date(date.now() - 1*60*60 * 1000)}}).count();

像这样更正:

from datetime import datetime, timedelta

...

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

此外,格式化您的代码,以便更好地理解:

import sys
from datetime import datetime, 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
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)

PS 请查看f-strings以更好地格式化您的uri

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM