allow_disk_use 无法处理 PyMongo 中的游标
allow_disk_use not working on cursor in PyMongo
>>> 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也是最新版本。
我提到了 Documentation and Source,但我无法理解我做错了什么。不应该与 Cursor
对象一起使用?如果有人能解释正确的方法以及为什么这不起作用,那就太好了。
在 pymongo 中,您可以将 allowDiskUse
与 aggregate
结合使用:
cve_ = colCVE.aggregate([{"$sort": {"Modified": -1}}], allowDiskUse=True)
从 3.11 版开始,您还可以将其传递给 find()
:
cve_ = colCVE.find(allow_disk_use=True).sort("Modified", pymongo.DESCENDING)
除了beachy的回答,升级到最新版本(3.11)后,
给出 AttributeError
的查询(在问题中提到)也恰好工作得很好。
>>> 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也是最新版本。
我提到了 Documentation and Source,但我无法理解我做错了什么。不应该与 Cursor
对象一起使用?如果有人能解释正确的方法以及为什么这不起作用,那就太好了。
在 pymongo 中,您可以将 allowDiskUse
与 aggregate
结合使用:
cve_ = colCVE.aggregate([{"$sort": {"Modified": -1}}], allowDiskUse=True)
从 3.11 版开始,您还可以将其传递给 find()
:
cve_ = colCVE.find(allow_disk_use=True).sort("Modified", pymongo.DESCENDING)
除了beachy的回答,升级到最新版本(3.11)后,
给出 AttributeError
的查询(在问题中提到)也恰好工作得很好。