我正在尝试对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"}
])
但是我得到了任何东西,似乎不起作用,知道我该怎么办?