killCursors

On this page本页内容

Definition定义

killCursors

Kills the specified cursor or cursors for a collection. 杀死集合的指定游标。MongoDB drivers use the killCursors command as part of the client-side cursor implementation.MongoDB驱动程序使用killCursors命令作为客户端游标实现的一部分。

Note注意

In general, applications should not use the killCursors command directly.通常,应用程序不应直接使用killCursors命令。

The killCursors command must be run against the database of the collection whose cursors you wish to kill.killCursors命令必须针对要删除其游标的集合的数据库运行。

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

The command has the following form:命令的格式如下:

db.runCommand( { "killCursors": <collection>, "cursors": [ <cursor id1>, ... ], comment: <any> } )
Field字段Type类型Description描述
killCursorsstringThe name of the collection.集合的名称。
cursorsarrayThe ids of the cursors to kill.要删除的游标的ID。
commentany

Optional. 可选。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类型(字符串、整数、对象、数组等)

New in version 4.4.在版本4.4中新增

Required Access所需访问权限

Kill Own Cursors杀死自己的游标

  • In MongoDB 4.2 and later, users can always kill their own cursors, regardless of whether the users have the privilege to killCursors. 在MongoDB 4.2及更高版本中,无论用户是否有权killCursors,用户都可以杀死自己的游标。Cursors are associated with the users at the time of cursor creation.游标在创建游标时与用户关联。
  • In MongoDB 3.6.3 through MongoDB 4.0.x, users require killCursors privilege to kill their own cursors. 在MongoDB 3.6.3到MongoDB 4.0x中,用户需要killCursors权限才能杀死自己的游标。Cursors are associated with the users at the time of cursor creation.游标在创建游标时与用户关联。

Kill Any Cursor杀死任何游标

If a user possesses the killAnyCursor privilege, that user may kill any cursor, even cursors created by other users.如果用户拥有killAnyCursor权限,则该用户可以杀死任何游标,甚至是其他用户创建的游标。

killCursors and Transactions和事务

Starting in MongoDB 4.2, you cannot specify killCursors as the first operation in a transaction.从MongoDB 4.2开始,不能将killCursors指定为事务中的第一个操作。

Example示例

Consider the following find operation on the test.restaurants collection:考虑对test.restaurants集合执行以下find操作:

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

which returns the following:其返回以下内容:

{
   "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
}

To kill this cursor, use the killCursors command.要终止此游标,请使用killCursors命令。

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

killCursors returns the following operation details:返回以下操作详细信息:

{
   "cursorsKilled" : [
      NumberLong("18314637080")
   ],
   "cursorsNotFound" : [ ],
   "cursorsAlive" : [ ],
   "cursorsUnknown" : [ ],
   "ok" : 1
}
←  getParameterkillOp →