Docs HomeMongoDB Manual

getMore

Definition定义

getMore

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

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 id.游标id。
collectionstringThe name of the collection over which the cursor is operating.游标在其上操作的集合的名称。
batchSizepositive integerOptional.可选的。The number of documents to return in the batch.批处理中要返回的文档数。
maxTimeMSnon-negative integerOptional.可选的。
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(). 可裁剪awaitData游标的getMore上的maxTimeMS被认为与maxAwaitTimeMS()相同。Drivers will only set this value on getMore for a tailable cursor on a capped collection with awaitData set to true. 对于awaitData设置为true的封顶集合上的可裁剪游标,驱动程序只会在getMore上设置此值。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.maxTimeMS由驱动程序为每次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. 在不可裁剪的游标上调用getMore时,不能设置maxTimeMSInstead, set it via maxTimeMS() when you create the cursor.相反,在创建游标时通过maxTimeMS()设置它。
  • To use getMore with maxTimeMS on a tailable cursor, enable awaitData when you create the cursor using cursor.tailable().要在可调整游标上使用getMoremaxTimeMS,请在使用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上的超时会保留游标中发生超时之前累积的文档。对于不可调整的游标,超时会引发错误。
commentanyOptional.可选的。
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类型(字符串、整数、对象、数组等)。
Note
If omitted, getMore inherits any comment set on the originating find or aggregate command. 如果省略,getMore将继承原始findaggregate命令上的任何注释集。

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 the one below:例如,在分片集群上的find操作创建的游标上运行getMore会返回一个类似于下面的文档:

{
"cursor" : {
"id" : NumberLong("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" : NumberLong("6813467763969884181")
}
}
}
Field字段Description描述
cursorContains 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 turned on, 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

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字段将添加到慢速查询日志消息探查器日志消息中。