对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

getMore(数据库命令)

getMore

与返回游标的命令结合使用,例如 find 和 aggregate,从游标返回下一个文档批处理。

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

注意

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

该命令具有以下语法:

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

该命令接受以下字段:

字段
类型
说明

getMore

long

游标标识符。

collection

字符串

游标所操作的集合的名称。

batchSize

正整数

可选.每个批处理返回的最大文档数量。

如果未设置 batchSize,则 getMore 为每个批处理返回最多 16 兆字 (MiB)。如果设置了 batchSize,则 getMore 返回 batchSize 个文档或 16 MiB 中较小的一个。

maxTimeMS

non-negative integer

可选。

指定服务器在 固定大小集合 上等待与 可追加游标 查询匹配的新文档的时长。对于可追加 awaitData 游标,getMore 上的 maxTimeMS 相当于maxAwaitTimeMS()。

驱动程序仅对固定大小集合上的可追加游标设置此值,其中 awaitData 为 true。否则,创建游标的命令会将 maxTimeMS 设置为初始操作和所有后续 getMore 操作的累积时间限制。对于将 awaitData 设置为 true 的可追加游标,以下情况成立:

  • 如果未提供值,则等待时间默认为 1(1000 毫秒)。

  • maxTimeMS 在 getMore 上指定 MongoDB 等待将新文档插入特定 getMore 命令的固定大小集合的最长时间。

  • maxTimeMS 由驱动程序为每次调用 getMore 单独设置。

MongoDB 使用与 db.killOp() 相同的机制终止超过时间限制的操作,但仅在指定的中断点处终止。

  • 在不可追加游标上调用 getMore 时,无法设置 maxTimeMS。相反,请在创建游标时使用 maxTimeMS() 进行设置。

  • 要在可追加游标上使用 getMore 和 maxTimeMS,请在使用 cursor.tailable() 创建游标时启用 awaitData。

  • 在创建游标的命令上设置 maxTimeMS 仅设置该操作的时间限制。使用 getMore 为进一步的操作设限。

  • 您可以为每次调用 getMore 而设置或者可以省略 maxTimeMS,并且不必使用相同的值。

  • 对于可追加游标,getMore 的超时会保留游标中发生超时之前累积的文档。对于不可追加游标,超时会引发错误。

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

如果省略,getMore 会继承在原始 find 或 aggregate 命令上设置的任何 comment。

该命令会返回包含游标信息和下一批处理的文档。

例如,在分片集群上通过 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")
}
}
}
字段
说明

cursor

包含游标信息,包括游标 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. 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.

postBatchResumeToken 字段可以与 $changeStream 管道一起使用,从此处开始或恢复变更流。

从 MongoDB 9.0 开始,您可以设置 internalOperationResponseMaxMS 参数,以便在长时间的变更流 oplog 扫描期间返回更新的 postBatchResumeToken,然后再由命令填充批处理或变更流到达 oplog 的末尾。

"ok"

表明命令是成功(1)还是失败(0)。

除这些字段外,db.runCommand() 响应还包括副本集和分片集群的以下信息:

  • $clusterTime

  • operationTime

请参阅 db.runCommand() 响应,了解详情。

如果启用了身份验证,那么您只能对您创建的游标运行 getMore。

对于在会话内创建的游标,无法在会话外调用 getMore。

对于在会话外创建的游标,无法在会话内调用 getMore。

对于多文档事务:

  • For cursors created outside of a transaction, you cannot call getMore inside the transaction.

  • For cursors created in a transaction, you cannot call getMore outside the transaction.

从 MongoDB 8.2 开始,游标标识符必须与在指定的 collection 上操作的游标名称匹配。如果不存在匹配的游标,getMore 将返回错误。

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 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。