如果我的问题很天真,请向我道歉,我是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.
据我所知,上面的代码使用了字符串而不是对象。因此,我想知道如何完成为数据库中的每个集合保留游标的任务,稍后我可以使用它来执行搜索和更新等操作。
最佳答案
如果您使用 pymongo 驱动程序,则必须使用 get_collection
方法或字典式查找代替。此外,您可能希望将 collection_names
中的 include_system_collections
设置为 False
因此您不包含系统集合(例如 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()
关于python - 如何使用Python提取Mongo中的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33131654/