【发布时间】:2019-09-01 08:40:14
【问题描述】:
我正在尝试获取 mongodb 中存在的所有数据库的值,遍历所有数据库和集合,而不是打印它的文档。我可以打印将集合作为变量传递的文档,但不能遍历所有数据库和集合(作为变量的值)。有人知道 pymongo 是否支持动态地作为值传递而不是将集合和数据库作为变量本身传递??
client = MongoClient('mongodb://localhost:27017/')
names = client.database_names()
for dbName in names:
print(dbName)
db = client.dbName
collectionNames = client[dbName].collection_names()
for colecao in collectionNames:
print(colecao)
cursor = db.colecao # choosing the collection you need
print(cursor)
cursor2 = cursor.find() # get documents
for document in cursor2:
pprint(document)
数据库名称和集合名称正常打印,但打印光标返回: "Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), u'dbName'), u'colecao')"
它与变量的名称一起使用。
【问题讨论】: