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 con- tains a 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: 

db.serverStatus() 

## Query Optimization 

Indexes improve the efficiency of read operations by reducing the amount of data that query operations need to process. This simplifies the work associated with fulfilling queries within MongoDB. 

