Database Manual / Reference / Query Language / CRUD Commands

getMore (database command)(数据库命令)

Definition定义

getMore

Use in conjunction with commands that return a cursor. For example, find and aggregate, to return subsequent batches of documents currently pointed to by the cursor.与返回游标的命令结合使用。例如,findaggregate,以返回游标当前指向的后续批次的文档。

Compatibility兼容性

This command 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. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关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的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
getMore: <long>,
collection: <string>,
batchSize: <int>,
maxTimeMS: <int>,
comment: <any>
}
)

Command Fields命令字段

The command accepts the following fields:该命令接受以下字段:

Field字段Type类型Description描述
getMorelongThe cursor identifier.
collectionstring字符串The name of the collection over which the cursor is operating.游标正在其上操作的集合的名称。
batchSizepositive integer

Optional. 可选。The maximum number of documents that can be returned in each batch of a query result.每批查询结果中可以返回的最大文档数。

If batchSize is not set, getMore returns up to 16 mebibytes (MiB) of data in all subsequent batches. If batchSize is set, getMore returns the lesser of batchSize documents or 16 MiB worth of documents.如果未设置batchSize,则getMore在所有后续批次中最多返回16兆字节(MiB)的数据。如果设置了batchSizegetMore将返回batchSize文档或价值16 MiB的文档中的较小者。

maxTimeMSnon-negative integer非负整数

Optional.可选。

Specifies the maximum time for the server to wait for new documents that match a tailable cursor query on a capped collection. 指定服务器等待与封顶集合上的可尾随游标查询匹配的新文档的最长时间。maxTimeMS on a getMore for a tailable awaitData cursor is considered the same as maxAwaitTimeMS(). getMore上的maxTimeMS对于可尾随awayData游标被认为与maxAwaitTimeMS()相同。Drivers will only set this value on getMore for a tailable cursor on a capped collection with awaitData set to true. 驱动程序只会在getMore上为awaitData设置为true的封顶集合上的可尾随游标设置此值。Otherwise, the command that creates the cursor sets maxTimeMS, which is the maximum amount of time that the initial operation, and any subsequent getMore operations, can spend cumulatively executing the query. 否则,创建游标的命令将设置maxTimeMS,这是初始操作和任何后续的getMore操作累积执行查询所花费的最大时间。For tailable cursors with awaitData set to true, the following is true:对于将awaitData设置为true的可缩放游标,以下内容为true

  • If no value is provided, the wait time defaults to 1 (1000 milliseconds).如果没有提供值,则等待时间默认为1(1000毫秒)。
  • maxTimeMS on getMore specifies the maximum amount of time MongoDB waits for new documents to be inserted into the capped collection for that specific getMore command.getMore上的maxTimeMS指定了MongoDB等待新文档插入特定getMore命令的上限集合的最长时间。
  • maxTimeMS is set individually by the driver for each call to getMore.由驱动程序为每次调用getMore单独设置。

MongoDB terminates operations that exceed their allotted time limit using the same mechanism as db.killOp(). MongoDB使用与db.killOp()相同的机制终止超过分配时间限制的操作。MongoDB only terminates an operation at one of its designated interrupt points.MongoDB仅在其指定的中断点之一终止操作。

  • You cannot set maxTimeMS when calling getMore on a non-tailable cursor. Instead, set it using maxTimeMS() when you create the cursor.在不可尾随的游标上调用getMore时,无法设置maxTimeMS。相反,在创建游标时使用maxTimeMS()设置它。
  • To use getMore with maxTimeMS on a tailable cursor, enable awaitData when you create the cursor using cursor.tailable().要在可尾随游标上使用带有maxTimeMSgetMore,请在使用cursor.tailable()创建游标时启用awaitData
  • Setting maxTimeMS on the command that creates a cursor only sets the time limit for that operation. Use getMore to set a limit on further operations.在创建游标的命令上设置maxTimeMS仅设置该操作的时间限制。使用getMore设置进一步操作的限制。
  • You can set or omit maxTimeMS for each call to getMore, and you don't have to use the same value.您可以为每次调用getMore设置或省略maxTimeMS,并且不必使用相同的值。
  • For a tailable cursor, a timeout on getMore retains the documents accumulated before the timeout occurred in the cursor. For a non-tailable cursor, a timeout raises an error.对于可跟踪游标,getMore上的超时保留了游标中发生超时之前累积的文档。对于不可跟踪的游标,超时会引发错误。
commentany任意

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

If omitted, getMore inherits any comment set on the originating find or aggregate command.如果省略,getMore将继承原始findaggregate命令上的任何comment集。

Output输出

The command returns a document that contains the cursor information as well as the next batch.该命令返回一个包含游标信息以及下一批的文档。

For example, running getMore on a cursor created by a find operation on a sharded cluster returns a document similar to this output:例如,在分片集群上通过find操作创建的游标上运行getMore会返回类似于以下输出的文档:

{
"cursor" : {
"id" : Long("678960441858272731"),
"ns" : "test.contacts",
"nextBatch" : [
{
"_id" : ObjectId("5e8e501e1a32d227f9085857"),
"zipcode" : "220000"
}
],
"partialResultsReturned" : true,
"postBatchResumeToken": "< Resume Token >"
},
"ok" : 1,
"operationTime" : Timestamp(1586385239, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1586385239, 2),
"signature" : {
"hash" : BinData(0,"lLjejeW6AQGReR9x1PD8xU+tP+A="),
"keyId" : Long("6813467763969884181")
}
}
}
Field字段Description描述
cursor

Contains the cursor information, including the cursor ID as well as the nextBatch of documents.包含游标信息,包括游标ID以及文档的nextBatch

If find (or subsequent getMore commands) returns partial results because the queried shard(s) aren't available, the find output includes a partialResultsReturned indicator field. 如果find(或后续的getMore命令)由于查询的分片不可用而返回部分结果,则find输出将包含partialResultsReturned指示符字段。If the queried shards are available for the initial find command, but one or more shards become unavailable for subsequent getMore commands, only the getMore commands that run while the shards aren't available include partialResultsReturned in their output.如果查询的分片可用于初始find命令,但一个或多个分片不可用于后续的getMore命令,则只有在分片不可用时运行的getMore指令才会在其输出中包含partialResultsReturned

The postBatchResumeToken field can be used with the $changeStream pipeline to start or resume a change stream from this point.postBatchResumeToken字段可以与$changeStream管道一起使用,以从此点启动或恢复更改流。

"ok"Indicates whether the command has succeeded (1) or failed (0).指示命令是成功(1)还是失败(0)。

In addition to these fields, the db.runCommand() response includes the following information for replica sets and sharded clusters:除了这些字段外,db.runCommand()响应还包括副本集和分片集群的以下信息:

  • $clusterTime
  • operationTime

See db.runCommand() Response for details.有关详细信息,请参阅db.runCommand()响应

Behavior行为

Access Control访问控制

If authentication is enabled, you can only run getMore against cursors you created.如果启用了身份验证,则只能对创建的游标运行getMore

Sessions会话

For cursors created inside a session, you cannot call getMore outside the session.对于在会话内创建的游标,不能在会话外调用getMore

Similarly, for cursors created outside of a session, you cannot call getMore inside a session.同样,对于在会话外创建的游标,您不能在会话内调用getMore

Transactions事务

For multi-document transactions:对于多文档事务

  • For cursors created outside of a transaction, you cannot call getMore inside the transaction.对于在事务外部创建的游标,您不能在事务内部调用getMore
  • For cursors created in a transaction, you cannot call getMore outside the transaction.对于在事务中创建的游标,您不能在事务外部调用getMore

Errors错误

Starting in MongoDB 8.2, the cursor identifier must match the name of the cursor that operates on the specified collection. 从MongoDB 8.2开始,游标标识符必须与操作指定collection的游标名称匹配。If the collection does not have a cursor with the specified cursor identifier, getMore returns an error.如果collection中没有具有指定游标标识符的游标,getMore将返回错误。

Slow Queries慢查询

Starting in MongoDB 5.1, when a getMore command is logged as a slow query, the queryHash and planCacheKey fields are added to the slow query log message and the profiler log message.从MongoDB 5.1开始,当getMore命令作为慢速查询记录时,queryHashplanCacheKey字段将添加到慢速查询日志消息分析器日志消息中。

Starting in MongoDB 8.0, the existing queryHash field is duplicated in a new field named planCacheShapeHash. If you're using an earlier MongoDB version, you'll only see the queryHash field. 从MongoDB 8.0开始,现有的queryHash字段被复制到一个名为planCacheShapeHash的新字段中。如果你使用的是早期的MongoDB版本,你只会看到queryHash字段。Future MongoDB versions will remove the deprecated queryHash field, and you'll need to use the planCacheShapeHash field instead.未来的MongoDB版本将删除弃用的queryHash字段,您将需要使用planCacheShapeHash字段。

Learn More了解更多