我正在尝试为mongodb中的数据库中的每个集合应用查询。数据库有140个集合,查询如下:
import pymongo
client=pymongo.MongoClient("localhost",27017)
db=client.EcoEmp_db
pipeline=[db.D12.aggregate([
{"$unwind":"$_id"},
{"$addFields":{
"bibliographic.link":"$link",
"bibliographic.keywords":"$keywords",
"bibliographic.abstract":"$abstract",
"bibliographic.author":"$author"
}},
{
"$replaceRoot":{"newRoot":"$bibliographic"}
},
{"$out": "D12_p"}
])]我从pymoongo运行了这个管道,并且工作正常。为了对每个集合进行迭代,我这样做:
import pymongo
if __name__ == '__main__':
connection = pymongo.MongoClient('localhost', 27017)
db = connection['EcoEmp_db']
collection = db.collection_names(include_system_collections=False)
for collect in collection:
db.db[collect].aggregate([
{"$unwind":"$_id"},
{"$addFields":{
"bibliographic.link":"$link",
"bibliographic.keywords":"$keywords",
"bibliographic.abstract":"$abstract",
"bibliographic.author":"$author"
}},
{
"$replaceRoot":{"newRoot":"$bibliographic"}
},
{"$out": collect+"_p"}
])但是我得到了任何东西,它似乎不起作用,你知道我怎么做吗?
发布于 2020-01-20 22:53:22
要获取字典格式的结果,其中your_db_name是您的数据库名称:
collections = your_db_name.collection_names()
for col in collections:
col_x = your_db_name[col].find()
for item in col_x:
print(item)https://stackoverflow.com/questions/57578396
复制相似问题