Kill long-running ops mongodb
List queries running >5s with ns then killOp each; note: killOp is async, verify after
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)})'
more mongodb
all 20 commands →
Query plan with stats
mongosh --eval 'db.<name>.find({status:"active"}).explain("executionStats").executionStats'
Unused index detection
mongosh --eval 'db.<name>.aggregate([{$indexStats:{}}]).forEach(i=>print(i.name,i.accesses.ops))'
Profile slow queries
mongosh --eval 'db.setProfilingLevel(1,{slowms:100}); db.system.profile.find().sort({millis:-1}).limit(5)'
Connect with mongosh
mongosh 'mongodb://user:pass@mongo:27017/dbname'
Replica set status
mongosh --eval 'rs.status()' mongodb://mongo:27017