Have you ever waited a while for a query to finish only to find out that it's not going to end in a reasonable time? Does your system consist of many shards?

 If you answered 'yes' to both questions, you probably know that killing a stuck query on all shards can be a very frustrating task because MongoDB's killOp database method will kill a query only on a single shard.

However, using the following script will kill the query on all shards (When connected to admin database. Run at your own risk):

db.currentOp({op:"query", "secs_running":{$gt:1200}}).inprog.forEach(function(obj){db.killOp(obj.opid);});

Explanation: This will kill all queries running for more than 20 minutes on all shards. You can, of course, filter by the original query parameters if you only want to kill your own query.

SIGN UP FOR OUR NEWSLETTER

Start receiving monthly updates on new features and the latest industry trends.

Please enable the javascript to submit this form