Replica set status mongodb
Per-member state and last applied optime; spot lagging or unhealthy secondaries
mongosh --eval 'rs.status().members.forEach(m=>print(m.name,m.stateStr,m.optimeDate))'
more mongodb
all 20 commands →
Kill long-running ops
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)})'
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'