I came across this thread looking for a solution to the following error in with pymongo
: CursorNotFound: Cursor not found, cursor id
. Obviously the reason is the same as this thread, but there is no solution provided for pymongo
.
From the FAQ in the pymongo documentation the following links answer the question: here and here.
The quickest solution is to disable the timeout, so the server does not expire the cursor, and it's always valid.
client = MongoClient("localhost")
db = client.testdatabase
cursor = db.testcollection.find({}, no_cursor_timeout=True)
In the find() documentation you can find alternatives by changing the cursor type:
cursor_type (optional): the type of cursor to return. The valid
options are defined by CursorType:
- NON_TAILABLE - the result of this find call will return a standard cursor over the result set.
- TAILABLE - the result of this find call will be a tailable cursor - tailable cursors are only for use with capped collections. They are not closed when the last data is retrieved but are kept open and the
cursor location marks the final document position. If more data is
received iteration of the cursor will continue from the last document
received. For details, see the tailable cursor documentation.
- TAILABLE_AWAIT - the result of this find call will be a tailable cursor with the await flag set. The server will wait for a few seconds
after returning the full result set so that it can capture and return
additional data added during the query.
- EXHAUST - the result of this find call will be an exhaust cursor. MongoDB will stream batched results to the client without waiting for
the client to request each batch, reducing latency. See notes on
compatibility below.
Update: After using the no_cursor_timeout
I was wondering what happeneded to the cursor when they are not executed. Here is the answer: PyMongo: What happens to cursor when no_cursor_timeout=True