- MongoDB CRUD Operations >
- MongoDB CRUD Concepts >
- Read Operations >
- Cursors
Cursors¶
On this page
In the mongo shell, the primary method for the read operation is the db.collection.find() method. This method queries a collection and returns a cursor to the returning documents.
To access the documents, you need to iterate the cursor. However, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times [1] to print up to the first 20 documents in the results.
For example, in the mongo shell, the following read operation queries the inventory collection for documents that have type equal to 'food' and automatically print up to the first 20 matching documents:
db.inventory.find( { type: 'food' } );
To manually iterate the cursor to access the documents, see Iterate a Cursor in the mongo Shell.
[1] | You can use the DBQuery.shellBatchSize to change the number of iteration from the default value 20. See Working with the mongo Shell for more information. |
Cursor Behaviors¶
Closure of Inactive Cursors¶
By default, the server will automatically close the cursor after 10 minutes of inactivity, or if client has exhausted the cursor. To override this behavior in the mongo shell, you can use the cursor.noCursorTimeout() method:
var myCursor = db.inventory.find().noCursorTimeout();
After setting the noCursorTimeout option, you must either close the cursor manually with cursor.close() or by exhausting the cursor’s results.
See your driver documentation for information on setting the noCursorTimeout option.
Cursor Isolation¶
As a cursor returns documents, other operations may interleave with the query. For the MMAPv1 storage engine, intervening write operations on a document may result in a cursor that returns a document more than once if that document has changed. To handle this situation, see the information on snapshot mode.
Cursor Batches¶
The MongoDB server returns the query results in batches. Batch size will not exceed the maximum BSON document size. For most queries, the first batch returns 101 documents or just enough documents to exceed 1 megabyte. Subsequent batch size is 4 megabytes. To override the default size of the batch, see batchSize() and limit().
For queries that include a sort operation without an index, the server must load all the documents in memory to perform the sort before returning any results.
As you iterate through the cursor and reach the end of the returned batch, if there are more results, cursor.next() will perform a getmore operation to retrieve the next batch. To see how many documents remain in the batch as you iterate the cursor, you can use the objsLeftInBatch() method, as in the following example:
var myCursor = db.inventory.find();
var myFirstDocument = myCursor.hasNext() ? myCursor.next() : null;
myCursor.objsLeftInBatch();
Cursor Information¶
The db.serverStatus() method returns a document that includes a metrics field. The metrics field contains a metrics.cursor field with the following information:
- number of timed out cursors since the last server restart
- number of open cursors with the option DBQuery.Option.noTimeout set to prevent timeout after a period of inactivity
- number of “pinned” open cursors
- total number of open cursors
Consider the following example which calls the db.serverStatus() method and accesses the metrics field from the results and then the cursor field from the metrics field:
db.serverStatus().metrics.cursor
The result is the following document:
{
"timedOut" : <number>
"open" : {
"noTimeout" : <number>,
"pinned" : <number>,
"total" : <number>
}
}
See also
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.