How do I abort a running query in the MongoDB shel

2019-03-17 05:51发布

问题:

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.

回答1:

Based on Alex's answer.

  1. Query current running operations:

    db.currentOp()
    

  1. Kill the operation based on opid

    db.killOp(30318806)
    


回答2:

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.



回答3:

For sharded cluster:

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

For example:

db.killOp("shard0000:3134616")