I am trying to iterate through this loop:
for doc in coll.find()
I get the following error at the 100,000th plus record.
File "build\bdist.win32\egg\pymongo\cursor.py", line 703, in next File "build\bdist.win32\egg\pymongo\cursor.py", line 679, in _refresh File "build\bdist.win32\egg\pymongo\cursor.py", line 628, in __send_message File "build\bdist.win32\egg\pymongo\helpers.py", line 95, in _unpack_response pymongo.errors.OperationFailure: cursor id '1236484850793' not valid at server
what does this error mean?
Approach 1: The cursor returned is an iterable, thus we can convert it into a list. If the length of the list is zero (i.e. List is empty), this implies the cursor is empty as well.
In MongoDB, when the find() method is used to find the documents present in the given collection, then this method returned a pointer which will points to the documents of the collection, now this pointer is known as cursor.
find() to search documents in collections then as a result it returns a pointer. That pointer is known as a cursor. Consider if we have 2 documents in our collection, then the cursor object will point to the first document and then iterate through all documents which are present in our collection.
Maybe your cursor timed out on the server. To see if this is the problem, try to set timeout=False`:
for doc in coll.find(timeout=False)
See http://api.mongodb.org/python/1.6/api/pymongo/collection.html#pymongo.collection.Collection.find
If it was a timeout problem one possible solution is to set the batch_size
(s. other answers).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With