cursor.batchSize()
On this page
Definition
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 maximum number of documents within each batch returned in a query result. By default, the initial batch size is
101
documents and subsequent batches have a maximum size of 16 mebibytes (MiB). This option can enforce a smaller limit than 16 MiB, but not a larger one. If you setbatchSize
to a limit that results in batches larger than 16 MiB, this option has no effect andbatchSize()
uses the default batch size.A
batchSize
of0
means that the cursor is established, but no documents are returned in the first 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:FieldTypeDescriptionsize
integer
The maximum number of documents to return for a batch. The default initial batch size is
101
documents and subsequent batches have a maximum size of 16 mebibytes (MiB). This option can enforce a smaller limit than 16 MiB, but not a larger one. The default applies to drivers and Mongo Shell. For details, see Cursor Batches.
Compatibility
This method is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Note
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Example
The following example sets batchSize
for the results of a query
(specifically, find()
) to 10
:
db.inventory.find().batchSize(10)