mongodb
20 commands
20 shown
Connect with mongosh
Open interactive shell to a MongoDB instance
mongosh 'mongodb://user:pass@mongo:27017/dbname'
Replica set status
Show replica set members, lag, and election status
mongosh --eval 'rs.status()' mongodb://mongo:27017
Database stats
Data size, storage size, collection count for a database
mongosh --eval 'db.stats()' mongodb://mongo:27017/mydb
Collection stats
Document count, index sizes, storage for a collection
mongosh --eval 'db.myCollection.stats()' mongodb://mongo:27017/mydb
Current operations
Show operations running longer than 5 seconds
mongosh --eval 'db.currentOp({active:true,secs_running:{$gt:5}})' mongodb://mongo:27017
Create dump
Export database to BSON files
mongodump --uri='mongodb://user:pass@mongo:27017' --db=mydb --out=/backups/
Restore dump
Import BSON backup into MongoDB
mongorestore --uri='mongodb://user:pass@mongo:27017' --db=mydb /backups/mydb/
List indexes
Show all indexes on a collection
mongosh --eval 'db.myCollection.getIndexes()' mongodb://mongo:27017/mydb
Connect with URI
Connect to Atlas or any MongoDB URI with mongosh
mongosh 'mongodb+srv://user:pass@cluster.example.com/mydb'
Explain query plan
Show execution stats including index usage
db.orders.find({status:'new'}).explain('executionStats')
Create compound index
Ascending userId + descending createdAt index
db.orders.createIndex({userId: 1, createdAt: -1}, {background: true})
Aggregation pipeline example
Top 10 users by paid order amount
db.orders.aggregate([{$match:{status:'paid'}},{$group:{_id:'$userId',total:{$sum:'$amount'}}},{$sort:{total:-1}},{$limit:10}])
Check replication lag
Show secondary lag and oplog window
rs.printSecondaryReplicationInfo()
Current operations (slow queries)
List operations running longer than 5 seconds
db.currentOp({active:true, secs_running:{$gt:5}})
Dump a database
Dump all collections in a database to BSON files
mongodump --uri='mongodb://localhost:27017' --db=mydb --out=/backup/$(date +%F)
Replica set status
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))'
Kill long-running ops
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)})'
Query plan with stats
Show docs examined vs returned and index usage; COLLSCAN means missing index
mongosh --eval 'db.<name>.find({status:"active"}).explain("executionStats").executionStats'
Unused index detection
Access count per index; zero ops = candidate to drop and reclaim write cost
mongosh --eval 'db.<name>.aggregate([{$indexStats:{}}]).forEach(i=>print(i.name,i.accesses.ops))'
Profile slow queries
Capture ops slower than 100ms then read top offenders from system.profile
mongosh --eval 'db.setProfilingLevel(1,{slowms:100}); db.system.profile.find().sort({millis:-1}).limit(5)'
no commands match