Database Manual / Reference / mongosh Methods / Cursors

cursor.batchSize() (mongosh method方法)

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.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

The maximum number of documents that can be returned in each batch of a query result. By default, the initial batch size is the lesser of 101 documents or 16 mebibytes (MiB) worth of documents. Subsequent batches have a maximum size of 16 MiB. 每批查询结果中可以返回的最大文档数。默认情况下,初始批大小为101个文档或价值16兆字节(MiB)的文档中的较小者。后续批次的最大容量为16 MiB。This option can enforce a smaller limit than 16 MiB, but not a larger one. When set, the batchSize is the lesser of batchSize documents or 16 MiB worth of documents.此选项可以强制执行小于16 MiB的限制,但不能强制执行更大的限制。设置后,batchSizebatchSize文档或价值16 MiB的文档中的较小值。

A batchSize of 0 means that the cursor is established, but no documents are returned in the first batch.batchSize为0表示游标已建立,但第一批中没有返回文档。

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.batchSize调整为适合数据库、负载和应用程序需求的值。

The batchSize() method takes the following field:batchSize()方法接受以下字段:

Field字段Type类型Description描述
sizeinteger整数The maximum number of documents that can be returned in each batch of a query result. 每批查询结果中可以返回的最大文档数。The default initial batch size is the lesser or 101 documents or 16 mebibytes (MiB) worth of documents. Subsequent batches have a maximum size of 16 MiB. This option can enforce a smaller limit than 16 MiB, but not a larger one. 默认的初始批大小是较小的,即101个文档或价值16兆字节(MiB)的文档。后续批次的最大容量为16 MiB。此选项可以强制执行小于16 MiB的限制,但不能强制执行更大的限制。The default applies to drivers and Mongo Shell. For details, see Cursor Batches.默认值适用于驱动程序和Mongo Shell。有关详细信息,请参阅游标批处理

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Example示例

The following example sets batchSize for the results of a query (specifically, find()) to 10:以下示例将查询结果(特别是find())的batchSize设置为10

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

Learn More了解更多