如何使用 Python 提取 Mongo 中的集合?

How to extract collections in Mongo using Python?

如果我的问题很幼稚,请向我道歉,我是 python 的新手,我正在尝试使用 pymongo 来使用集合。我尝试使用

提取名称
collects = db.collection_names(); #This returns a list with names of collections

但是当我尝试使用

获取光标时
cursor = db.collects[1].find(); #This returns a cursor which has no reference to a collection. 

我了解到上面的代码使用了字符串而不是对象。所以,我想知道如何完成为数据库中的每个集合保留游标的任务,稍后我可以使用它来执行搜索和更新等操作。

对不起,我还不能创建评论,但你试过了吗?:

cursor = db.getCollection(collects[1]).find();

如果您使用的是 pymongo 驱动程序,则必须使用 get_collection method or a dict-style lookups instead. Also you may want to set the include_system_collections to False in collection_names,这样您就不会包含系统集合(例如 system.indexes)

import pymongo

client = pymongo.MongoClient()
db = client.db
collects = db.collection_names(include_system_collections=False)
cursor = db.get_collection(collects[1]).find()

cursor = db[collects[1]].find()