【问题标题】:allow_disk_use not working on cursor in PyMongoallow_disk_use 在 PyMongo 中的光标上不起作用
【发布时间】:2021-02-02 02:38:09
【问题描述】:
>>> from pymongo import MongoClient
>>> client = MongoClient()
>>> db = client['cvedb']
>>> db.list_collection_names()
['cpeother', 'mgmt_blacklist', 'via4', 'capec', 'cves', 'mgmt_whitelist', 'ranking', 'cwe', 'info', 'cpe']
>>> colCVE = db["cves"]

>>> cve = colCVE.find().sort("Modified", -1) # this works

>>> cve_ = colCVE.find().allow_disk_use(True).sort("Modified", -1) # this doesn't work
AttributeError: 'Cursor' object has no attribute 'allow_disk_use'
>>> cve_ = colCVE.find().sort("Modified", -1).allow_disk_use(True) # this doesn't work
AttributeError: 'Cursor' object has no attribute 'allow_disk_use'
>>> cve.allow_disk_use(True) # this doesn't work
AttributeError: 'Cursor' object has no attribute 'allow_disk_use'
>>>

我想使用allow_disk_use() 方法但得到上述错误。我的 MongoDB 服务器是 4.4.1,pymongo 也是最新版本。

我提到了DocumentationSource,但我不知道我做错了什么。不应该与Cursor 对象一起使用吗?如果有人能解释正确的方法以及为什么这不起作用,那就太好了。

【问题讨论】:

    标签: python python-3.x mongodb pymongo pymongo-3.x


    【解决方案1】:

    除了beachy的回答,升级到最新版本(3.11)后, 给出AttributeError(问题中提到)的查询也恰好可以正常工作。

    【讨论】:

      【解决方案2】:

      在 pymongo 中,您可以将allowDiskUseaggregate 结合使用:

      cve_ = colCVE.aggregate([{"$sort": {"Modified": -1}}], allowDiskUse=True)
      

      从 3.11 版本开始,您也可以将其传递给 find():

      cve_ = colCVE.find(allow_disk_use=True).sort("Modified", pymongo.DESCENDING)
      

      【讨论】:

      • 谢谢,成功了!有没有关于它仅适用于聚合的文档?你是怎样找到它的?根据这个docs.mongodb.com/manual/reference/method/cursor.allowDiskUse/…,它应该在光标上工作
      • 我必须纠正自己。请参阅我编辑的答案。您必须将其传递给 find-command 而不是 sort()。
      • sort 中的一个参数似乎不起作用,TypeError: __init__() got an unexpected keyword argument 'allow_disk_use'
      • 你必须通过 'allow_disk_use' 来查找,而不是排序,正如我的回答中所建议的那样。
      • 是的,我通过查找并得到了上面评论中提到的错误
      猜你喜欢
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多