无法打印pymong中集合的值

2024-09-30 20:22:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图获取mongodb中所有数据库的值,遍历所有数据库和集合,而不是打印it文档。我可以打印将集合作为变量传递的文档,但不能遍历所有数据库和集合(作为变量的值)。有人知道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)

数据库名称和集合名称正常打印,但打印光标返回: “集合(数据库)MongoClient(主机=['本地主机:27017'],文档\u class=dict,tz \u aware=False,connect=True),u'dbName'),u'colecao')”

它与变量的名称一起使用。你知道吗


Tags: in文档名称client数据库fordbnames