Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a mongodb cursor will expire

Tags:

mongodb

I have no knowledge of mongodb and I just want to ask if something is possible and, if possible, how it can be done. My question is how can we know when a cursor will expire. Is there any API for this purpose?

I would be grateful for any comments and recommendations.

Best regards.

like image 301
Georgi Naumov Avatar asked Feb 18 '14 12:02

Georgi Naumov


3 Answers

From the MongoDB documentation:

By default, MongoDB will automatically close a cursor when the client has exhausted all results in the cursor. However, for capped collections you may use a Tailable Cursor that remains open after the client exhausts the results in the initial cursor.

http://docs.mongodb.org/manual/tutorial/create-tailable-cursor/

Other factors which could cause a cursor to expire are the batchSize and timeout. To sum it up factors which expire the cursor are:

  • result exhausation
  • batchSize: http://docs.mongodb.org/manual/reference/method/cursor.batchSize/
  • timeout: http://api.mongodb.org/java/2.6/com/mongodb/MongoOptions.html
like image 67
Jon Avatar answered Sep 30 '22 01:09

Jon


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

like image 21
aitorhh Avatar answered Sep 30 '22 01:09

aitorhh


Ordinarily, a cursor "dies" on the database server after a certain length of time (approximately 10 minutes) and will be closed when the client has exhausted all of the results.

Some drivers have an "immortal" option, which does the equivalent in the Java driver of setting a NoTimeout option:

dbcoll.find(...).addOption(Bytes.QUERYOPTION_NOTIMEOUT) 

Set in a manner like above if you intend to keep an open cursor over this period of time.

http://api.mongodb.org/java/current/com/mongodb/Bytes.html#QUERYOPTION_NOTIMEOUT

like image 37
Neil Lunn Avatar answered Sep 30 '22 01:09

Neil Lunn