定义
killCursorsKills the specified cursor or cursors for a collection. MongoDB drivers use the
killCursorscommand 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.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { killCursors: <collection>, cursors: [ <cursor id1>, ... ], comment: <any> } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 集合的名称。 |
| 阵列 | 要终止的游标的 ID。 |
| any | 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 |
必需的访问权限
终止自身游标
无论用户是否具有killCursors 权限,他们始终可以终止自己的游标。创建游标时,游标与用户相关联。
终止任何游标
如果用户具有killAnyCursor 权限,则他们可以终止任何用户创建的游标。
killCursors 和事务
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 }