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

2024-05-13 11:07:34 发布

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

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

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

但当我试图让光标

^{pr2}$

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


Tags: of代码dbnameswiththiscollectionslist
2条回答

如果使用pymongo驱动程序,则必须使用^{}方法或dict样式的查找。另外,您可能希望在^{}中将include_system_collections设置为False,这样就不包括系统集合(例如系统索引)在

import pymongo

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

或者

^{pr2}$

Sry,我还不能发表评论,但你试过了吗?公司名称:

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

相关问题 更多 >