mongodb commands
20 commands, each with a one-line explanation. Click a command to copy it, or its title for details.
Commands 20 shown
-
mongosh 'mongodb://user:pass@mongo:27017/dbname'Open interactive shell to a MongoDB instance
-
mongosh --eval 'rs.status()' mongodb://mongo:27017Show replica set members, lag, and election status
-
mongosh --eval 'db.stats()' mongodb://mongo:27017/mydbData size, storage size, collection count for a database
-
mongosh --eval 'db.myCollection.stats()' mongodb://mongo:27017/mydbDocument count, index sizes, storage for a collection
-
mongosh --eval 'db.currentOp({active:true,secs_running:{$gt:5}})' mongodb://mongo:27017Show operations running longer than 5 seconds
-
mongodump --uri='mongodb://user:pass@mongo:27017' --db=mydb --out=/backups/Export database to BSON files
-
mongorestore --uri='mongodb://user:pass@mongo:27017' --db=mydb /backups/mydb/Import BSON backup into MongoDB
-
mongosh --eval 'db.myCollection.getIndexes()' mongodb://mongo:27017/mydbShow all indexes on a collection
-
mongosh 'mongodb+srv://user:pass@cluster.example.com/mydb'Connect to Atlas or any MongoDB URI with mongosh
-
db.orders.find({status:'new'}).explain('executionStats')Show execution stats including index usage
-
db.orders.createIndex({userId: 1, createdAt: -1}, {background: true})Ascending userId + descending createdAt index
-
db.orders.aggregate([{$match:{status:'paid'}},{$group:{_id:'$userId',total:{$sum:'$amount'}}},{$sort:{total:-1}},{$limit:10}])Top 10 users by paid order amount
-
rs.printSecondaryReplicationInfo()Show secondary lag and oplog window
-
db.currentOp({active:true, secs_running:{$gt:5}})List operations running longer than 5 seconds
-
mongodump --uri='mongodb://localhost:27017' --db=mydb --out=/backup/$(date +%F)Dump all collections in a database to BSON files
-
mongosh --eval 'rs.status().members.forEach(m=>print(m.name,m.stateStr,m.optimeDate))'Per-member state and last applied optime; spot lagging or unhealthy secondaries
-
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)})'List queries running >5s with ns then killOp each; note: killOp is async, verify after
-
mongosh --eval 'db.<name>.find({status:"active"}).explain("executionStats").executionStats'Show docs examined vs returned and index usage; COLLSCAN means missing index
-
mongosh --eval 'db.<name>.aggregate([{$indexStats:{}}]).forEach(i=>print(i.name,i.accesses.ops))'Access count per index; zero ops = candidate to drop and reclaim write cost
-
mongosh --eval 'db.setProfilingLevel(1,{slowms:100}); db.system.profile.find().sort({millis:-1}).limit(5)'Capture ops slower than 100ms then read top offenders from system.profile
no commands match — try a different search