killCursors
New in version 3.2.
Definition
killCursors
- Kills the specified cursor or cursors for a collection. MongoDBdrivers use the
killCursors
command as part of theclient-side cursor implementation.
Note
In general, applications should not use thekillCursors
command directly.
The killCursors
command must be run against the database of thecollection whose cursors you wish to kill.
To run killCursors, use the db.runCommand( { <command> } )
method.
The command has the following form:
- db.runCommand( { "killCursors": <collection>, "cursors": [ <cursor id1>, ... ] } )
FieldTypeDescriptionkillCursors
stringThe name of the collection.cursors
arrayThe ids of the cursors to kill.
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
. Cursors are associated with the users atthe time of cursor creation. - In MongoDB 3.6.3 through MongoDB 4.0.x, users require
killCursors
privilege to kill their own cursors.Cursors are associated with the users at the time of cursor creation.
Kill Any Cursor
If a user possesses the killAnyCursor
privilege, thatuser may kill any cursor, even cursors created by other users.
killCursors and Transactions
Starting in MongoDB 4.2, you cannot specify killCursors
asthe first operation in a transaction.
Example
Consider the following find
operation on thetest.restaurants
collection:
- 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.
- use test
- db.runCommand( { killCursors: "restaurants", cursors: [ NumberLong("18314637080") ] } )
killCursors
returns the following operation details:
- {
- "cursorsKilled" : [
- NumberLong("18314637080")
- ],
- "cursorsNotFound" : [ ],
- "cursorsAlive" : [ ],
- "cursorsUnknown" : [ ],
- "ok" : 1
- }