cursor.batchSize()

On this page本页内容

Definition定义

cursor.batchSize(size)
Important重要
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

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

Specifies the number of documents to return in each batch of the response from the MongoDB instance. 指定MongoDB实例响应的每个批中要返回的文档数。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.在大多数情况下,修改批大小不会影响用户或应用程序,因为mongosh和大多数驱动程序都会返回结果,就像MongoDB返回单个批一样。

The batchSize() method takes the following parameter:batchSize()方法采用以下参数:

Parameter参数Type类型Description描述
sizeintegerThe number of documents to return per batch. 每批要返回的文档数。Do not use a batch size of 1.不要使用批次大小1
Note注意

Specifying 1 or a negative number is analogous to using the limit() method.指定1或负数类似于使用limit()方法。

Example示例

The following example sets the batch size for the results of a query (i.e. find()) to 10. 以下示例将查询结果的批大小(即find())设置为10The batchSize() method does not change the output in mongosh, which, by default, iterates over the first 20 documents.batchSize()方法不会更改mongosh中的输出,默认情况下,mongosh会迭代前20个文档。

db.inventory.find().batchSize(10)
←  cursor.allowPartialResults()cursor.close() →