Docs Menu
Docs Home
/
MongoDB Manual
/ / /

cursor.batchSize()

On this page

  • Definition
  • Example
  • Learn More
cursor.batchSize(size)

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

Specifies the number of documents to return in each batch of the response from the MongoDB instance. In most cases, modifying the batch size will not affect the user or the application, as mongosh and most drivers return results as if MongoDB returned a single batch.

Note

If the batch size is too large, the cursor allocates more resources than it requires, which can negatively impact query performance. If the batch size is too small, the cursor requires more network round trips to retrieve the query results, which can negatively impact query performance.

Adjust batchSize to a value appropriate to your database, load, and application needs.

The batchSize() method takes the following field:

Field
Type
Description
size
integer
The initial number of documents to return for a batch. The default initial batch size is 101 documents. Subsequent batches are 16 megabytes. The default applies to drivers and Mongo Shell. For details, see Cursor Batches.

The following example sets batchSize for the results of a query (specifically, find()) to 10:

db.inventory.find().batchSize(10)

Back

cursor.allowPartialResults