我想对所有数据库运行相同的查询。
`
例如
import pymongo
db = pymongo.MongoClient("localhost")
db["*"]["test"].find()或
import pymongo
db = pymongo.MongoClient("localhost")
db["db1","db2","db3"]["test"].find()坦率地说,如何在mongodb中运行此逻辑?
import pymongo
db = pymongo.MongoClient("localhost")
db_names = db.list_database_names()
for x in db_names:
db[x]["test"].find()发布于 2022-11-28 22:06:02
在单个命令中没有在多个数据库上运行相同查询的命令。
您可以通过迭代list_database_names()方法来获得数据库列表并重复相同的命令,您可以对MongoClient实例运行该方法。
import pymongo
client = pymongo.MongoClient()
collection = 'mycollection'
for x in client.list_database_names():
print(client[x][collection].count_documents({}))https://stackoverflow.com/questions/74606515
复制相似问题