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

killCursors(数据库命令)

killCursors

Kills the specified cursor or cursors for a collection. MongoDB drivers use the killCursors command as part of the client-side cursor implementation.

警告

应用程序通常不应直接运行killCursors 命令。相反,让驱动程序自动处理游标管理。

必须针对要终止其游标的集合的数据库运行killCursors命令。

要运行 killCursors,请使用db.runCommand( { <command> } )方法。

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

  • 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(
{
killCursors: <collection>,
cursors: [ <cursor id1>, ... ], comment: <any>
}
)

该命令接受以下字段:

字段
类型
说明

killCursors

字符串

集合的名称。

cursors

阵列

要终止的游标的 ID。

comment

any

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

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

无论用户是否具有killCursors 权限,他们始终可以终止自己的游标。创建游标时,游标与用户相关联。

如果用户具有killAnyCursor 权限,则他们可以终止任何用户创建的游标。

You cannot specify the killCursors command as the first operation in a transaction.

此外,如果在ACID 事务中运行killCursors 命令,服务器会立即停止指定的游标。它不会等待ACID 事务提交。

考虑对collection执行以下find test.restaurants操作:

use test
db.runCommand(
{ find: "restaurants",
filter: { stars: 5 },
projection: { name: 1, rating: 1, address: 1 },
sort: { name: 1 },
batchSize: 5
}
)

返回以下内容:

{
"waitedMS" : Long(0),
"cursor" : {
"firstBatch" : [
{
"_id" : ObjectId("57506d63f578028074723dfd"),
"name" : "Cakes and more"
},
{
"_id" : ObjectId("57506d63f578028074723e0b"),
"name" : "Pies and things"
},
{
"_id" : ObjectId("57506d63f578028074723e1d"),
"name" : "Ice Cream Parlour"
},
{
"_id" : ObjectId("57506d63f578028074723e65"),
"name" : "Cream Puffs"
},
{
"_id" : ObjectId("57506d63f578028074723e66"),
"name" : "Cakes and Rolls"
}
],
"id" : Long("18314637080"),
"ns" : "test.restaurants"
},
"ok" : 1
}

To kill this cursor, use the killCursors command.

use test
db.runCommand( { killCursors: "restaurants", cursors: [ Long("18314637080") ] } )

killCursors 返回以下操作详细信息:

{
"cursorsKilled" : [
Long("18314637080")
],
"cursorsNotFound" : [ ],
"cursorsAlive" : [ ],
"cursorsUnknown" : [ ],
"ok" : 1
}