gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:26:02 24 4
gpt4 key购买 nike

如果我的问题很天真,请向我道歉,我是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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com