Kill long-running operations

Find operations running longer than 5s and kill each with killOp. killOp is async, so check the result afterwards

mongosh --eval 'db.currentOp({secs_running:{$gt:5},op:{$ne:"none"}}).inprog.forEach(o=>{print(o.opid,o.secs_running,o.ns); db.killOp(o.opid)})'

tool
mongodb20 commands
tags
mongodbtroubleshootingperformancedebug

More mongodb commands

  • mongosh --eval 'db.<name>.find({status:"active"}).explain("executionStats").executionStats'

    Compare documents examined and returned and check index usage. COLLSCAN means there is no index

  • mongosh --eval 'db.<name>.aggregate([{$indexStats:{}}]).forEach(i=>print(i.name,i.accesses.ops))'

    Show how often each index is used. An index with zero ops is a candidate to drop and makes writes cheaper

  • mongosh --eval 'db.setProfilingLevel(1,{slowms:100}); db.system.profile.find().sort({millis:-1}).limit(5)'

    Record operations slower than 100ms, then show the 5 slowest from system.profile

  • mongosh 'mongodb://user:pass@mongo:27017/dbname'

    Open an interactive shell to a MongoDB instance

  • mongosh --eval 'rs.status()' mongodb://mongo:27017

    Show replica set members, lag and election status

all 20 mongodb commands