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:

  1. db.runCommand( { "killCursors": <collection>, "cursors": [ <cursor id1>, ... ] } )

FieldTypeDescriptionkillCursorsstringThe name of the collection.cursorsarrayThe 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 tokillCursors. Cursors are associated with the users atthe time of cursor creation.
  • In MongoDB 3.6.3 through MongoDB 4.0.x, users requirekillCursors 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:

  1. use test
  2. db.runCommand(
  3. { find: "restaurants",
  4. filter: { stars: 5 },
  5. projection: { name: 1, rating: 1, address: 1 },
  6. sort: { name: 1 },
  7. batchSize: 5
  8. }
  9. )

which returns the following:

  1. {
  2. "waitedMS" : NumberLong(0),
  3. "cursor" : {
  4. "firstBatch" : [
  5. {
  6. "_id" : ObjectId("57506d63f578028074723dfd"),
  7. "name" : "Cakes and more"
  8. },
  9. {
  10. "_id" : ObjectId("57506d63f578028074723e0b"),
  11. "name" : "Pies and things"
  12. },
  13. {
  14. "_id" : ObjectId("57506d63f578028074723e1d"),
  15. "name" : "Ice Cream Parlour"
  16. },
  17. {
  18. "_id" : ObjectId("57506d63f578028074723e65"),
  19. "name" : "Cream Puffs"
  20. },
  21. {
  22. "_id" : ObjectId("57506d63f578028074723e66"),
  23. "name" : "Cakes and Rolls"
  24. }
  25. ],
  26. "id" : NumberLong("18314637080"),
  27. "ns" : "test.restaurants"
  28. },
  29. "ok" : 1
  30. }

To kill this cursor, use the killCursors command.

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

killCursors returns the following operation details:

  1. {
  2. "cursorsKilled" : [
  3. NumberLong("18314637080")
  4. ],
  5. "cursorsNotFound" : [ ],
  6. "cursorsAlive" : [ ],
  7. "cursorsUnknown" : [ ],
  8. "ok" : 1
  9. }