47

I can't believe I have to ask this, but how do I stop a query I just ran, which is now running, and will obviously take a very long time to complete in the Mongo shell? Control+C appears to crash the shell, and spits out a ton of errors. The silly solutions suggested in this post of course do not do anything. I understand that I could like open up another terminal tab and run db.currentOp(), find the operation ID, and then run db.killOp(), but I can't believe that's the only solution. I must be missing something obvious.

Community
  • 1
  • 1
collisionTwo
  • 899
  • 2
  • 12
  • 18
  • 1
    hmm ctrl+c breaking console sounds bad, that's what I normally use – Sammaye Nov 14 '14 at 23:52
  • 4
    Plus, Ctrl+C does _not_ necessarily kill the op. Bottom line: Besides killOp, the only reliable way to stop an operation is to stop the cluster. – Markus W Mahlberg Nov 15 '14 at 07:48
  • Use `db.currentOp()` and then `db.killOp()`. Ctrl-c and that other question have nothing to do with cancelling an operation that is already running on the database. – wdberkeley Nov 17 '14 at 02:17
  • @wdberkeley So MongoDB does not kill the op when it detects a SIGTERM? Sounds counter-intuitive to me – Sammaye Nov 17 '14 at 10:24
  • 10
    It's not just counter-intuitive, it's an extremely poor UI decision on the part of the Mongo team. Obviously I must be using Mongo "wrong", because generally when I run a query, and realize in a few seconds that the query will take far longer than it should (perhaps an index was forgotten; perhaps I'm testing a complicated compound index, perhaps I made a typo and it's now searching for some nonexistent unindexed field) **I want to kill the query and return to my Mongo shell**. It is bizarre to me that Mongo doesn't do this on Control-C like every other mainstream database software and shell. – collisionTwo Nov 25 '14 at 16:51
  • Control+D from this post http://stackoverflow.com/questions/10953034/how-do-i-abort-a-query-in-mongo-javascript-shell – Ravvy Jan 06 '16 at 22:25
  • @collisionTwo, see my answer please – Alex Erygin Apr 05 '16 at 15:01
  • @collisionTwo This question has been answered. Please, accept an answer as the correct one, so everyone can see them :) – makeMonday May 17 '16 at 13:35

3 Answers3

57

Based on Alex's answer.

  1. Query current running operations:

    db.currentOp()
    

Mongo currentOp Response

  1. Kill the operation based on opid

    db.killOp(30318806)
    
Roozbeh Zabihollahi
  • 7,207
  • 45
  • 39
  • Works great at mongodb 3.4 – Thiago Pereira May 26 '17 at 20:23
  • doesn't work for a sharded cluster. I get this error : "op had the wrong type. Expected string, found double" – Koustuv Sinha Jul 07 '17 at 15:13
  • Why does it work this way? I have to open a new terminal window or use screen to create a new session.. authenticate to the admin user only.. run db.corruentOp(), then copy and paste the opid.. then run db.killOp and paste the opid. Around a full minute vs just hitting CTRL+ C in less than a fraction of a second like I can in other database CLIs. – Nick Woodhams Nov 10 '20 at 07:11
12

According to Mongo documentation, you should:

  1. Obtain the current operation ID via db.currentOp()
  2. Kill opp via db.killOp()

Good example script can be found here.

Community
  • 1
  • 1
Alex Erygin
  • 3,161
  • 1
  • 22
  • 22
5

For sharded cluster:

  1. db.currentOp()
  2. db.killOp("shard_id:opid")

For example:

db.killOp("shard0000:3134616")
Yuriy Bosov
  • 91
  • 2
  • 5