Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I abort a running query in the MongoDB shell?

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.

like image 339
collisionTwo Avatar asked Nov 14 '14 23:11

collisionTwo


People also ask

How do I come out of MongoDB?

Edit: Use ctrl+d, which "kills the shell" according to http://www.gotothings.com/unix/bash-hotkeys.htm. If you hit this once after hitting enter in the middle of typing a command in the mongo shell, it will exit your continuation and get you back to a clean mongo prompt. Hitting it twice will exit the mongo shell.

How can I see what queries are running in MongoDB?

Summary. The db. currentOp() command provides means to find all the queries that are currently running on your MongoDB server. The optional fields can be further leveraged to help filter the results to specific criteria.


2 Answers

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) 
like image 116
Roozbeh Zabihollahi Avatar answered Oct 30 '22 04:10

Roozbeh Zabihollahi


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.

like image 35
Alex Erygin Avatar answered Oct 30 '22 04:10

Alex Erygin