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

用于返回MongoDB集合中最近1小时添加的记录的Python脚本,python,mongodb,Python,Mongodb,上述脚本在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

上述脚本在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)


另外,请查看以更好地格式化您的
uri

日期表达式错误,请尝试
new date(new date().getTime()-1*60*60*1000)
datetime.utcnow()-timedelta(hours=1)
(我从未使用过python)上面的查询给出零值,不确定出了什么问题上面的python脚本运行正常,但对于最近1小时的更新记录,其给定值为0;不确定上面的脚本出了什么问题;如果我在mongodb上手动运行查询,它会显示正确的编号