[SERVER-8968] Best practices for using mongodb cursor Created: 13/Mar/13  Updated: 01/Apr/13  Resolved: 26/Mar/13

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Critical - P2
Reporter: Kurt Agius Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Mongodb with nodejs driver


Participants:

 Description   

When I do a find, we get a new cursor from mongo.

I never close these cursors, i leave it to mongo to handle everything.
But if I 'force close' the cursor after every find (since i never re-use it and i have thousands of finds per second), will that improve performance or it just doesn't matter ?

Also is it worth implementing logic to re-use a cursor (because technically i do 'very similar queries' over an over again) ? Or getting a new cursor is extremely fast and not expensive at all ?



 Comments   
Comment by Kurt Agius [ 25/Mar/13 ]

Thanks a lot for your detailed description. I like the idea of the 'take-a-number tabs at a deli' i think its a perfect example to describe cursors.

I think this 'issue' is 'solved'. (next time i'll use stack overflow for questions)

Comment by Mary Burak [ 25/Mar/13 ]

A cursor is a temporary work area that is created when the db.collection.find() function is run. This temporary work area is used to store the documents retrieved from the database so they can be manipulated on the fly. Such manipulations can include things like sort(), limit(), foreach(), etc. Due to their ephemeral nature, cursors cannot be re-used. (An analogy given to me is that cursors are like the take-a-number tabs at a deli. Once your number has been called, the tab is no longer relevant.)

Cursors are automatically closed by the mongo server when they’re inactive for more than 10 minutes, or if the client has exhausted the cursor. The MongoDB docs recommend either closing cursors, or ensuring they’re exhausted by the client.

http://docs.mongodb.org/manual/core/read-operations/#cursor-behaviors

Unfortunately, I currently don’t have a large enough database to run a benchmark test to see how much of a performance impact cursors have on system resources. But, it might be a fun exercise.

Generated at Thu Feb 08 03:18:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.