allow_disk_use not working on cursor in PyMongo

1k Views Asked by At
>>> 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'
>>>

I want to use allow_disk_use() method but getting the above-mentioned error. My MongoDB server is 4.4.1 and pymongo is also at the latest version.

I have referred to Documentation and Source but I am not able to get what am I doing wrong. Is not supposed to work with Cursor objects? it would be good if anyone could explain the correct approach and why this isn't working.

2

There are 2 best solutions below

6
On BEST ANSWER

In pymongo, you can use allowDiskUse in combination with aggregate:

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

Since version 3.11 you can also pass it to find():

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

In addition to beachy's answer, after upgrading to the latest version(3.11), the queries which were giving AttributeError (mentioned in the question) also happens to work perfectly.