killCursors
定义
killCursors
杀死指定集合的游标或游标。MongoDB驱动程序使用
killCursors
命令作为客户端游标实现的一部分。警告
应用程序通常不应直接运行
killCursors
命令。相反,让驱动程序自动处理游标管理。killCursors
命令必须在要杀死游标的集合的数据库上运行。要运行killCursors,使用
db.runCommand( { <command> } )
方法。
兼容性
此命令在以下环境中托管的部署中可用
MongoDB Atlas:云中MongoDB部署的全托管服务
注意
此命令在所有MongoDB Atlas集群中受支持。有关Atlas对所有命令的支持信息,请参阅不受支持的命令。
MongoDB Enterprise:基于订阅的自托管MongoDB版本
MongoDB Community:源代码可用的、免费使用和自托管的MongoDB版本
语法
该命令具有以下语法
db.runCommand( { killCursors: <collection>, cursors: [ <cursor id1>, ... ], comment: <any> } )
命令字段
该命令包含以下字段
字段 | 类型 | 描述 |
---|---|---|
killCursors | string | 集合名称。 |
cursors | array | 要杀死的游标ID。 |
comment | any | 可选。用户提供的附加到此命令的注释。一旦设置,此注释将出现在以下位置的此命令记录旁边
注释可以是任何有效的 BSON 类型(字符串、整数、对象、数组等)。 |
所需访问权限
杀死自己的游标
用户始终可以杀死自己的游标,无论他们是否有 killCursors
权限。游标在游标创建时与用户关联。
杀死任何游标
如果用户有 killAnyCursor
权限,他们可以杀死任何用户创建的游标。
killCursors
和事务
您不能在事务中将 killCursors
命令指定为第一个操作。
此外,如果在事务中运行 killCursors
命令,服务器将立即停止指定的游标。它不会等待事务提交。
示例
考虑以下对 test.restaurants
集合的 find
操作
use test db.runCommand( { find: "restaurants", filter: { stars: 5 }, projection: { name: 1, rating: 1, address: 1 }, sort: { name: 1 }, batchSize: 5 } )
它返回以下内容
{ "waitedMS" : NumberLong(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" : NumberLong("18314637080"), "ns" : "test.restaurants" }, "ok" : 1 }
要终止此游标,请使用 killCursors
命令。
use test db.runCommand( { killCursors: "restaurants", cursors: [ NumberLong("18314637080") ] } )
killCursors
返回以下操作详细信息
{ "cursorsKilled" : [ NumberLong("18314637080") ], "cursorsNotFound" : [ ], "cursorsAlive" : [ ], "cursorsUnknown" : [ ], "ok" : 1 }