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

collisionTwo picture collisionTwo · Nov 15, 2014 · Viewed 22.7k times · Source

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.

Answer

Roozbeh Zabihollahi picture Roozbeh Zabihollahi · Apr 1, 2016

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)