Connect with a URI and open the target database directly
mongosh "mongodb://app:<PASSWORD>@10.0.0.12:27017/appdb?authSource=admin"Frequent mongosh commands for connections, CRUD, indexes and execution stats, replica set and sharding status, plus mongodump backups.
Connect with a URI and open the target database directly
mongosh "mongodb://app:<PASSWORD>@10.0.0.12:27017/appdb?authSource=admin"Log in with separate flags so the password is prompted instead of stored
mongosh --host 10.0.0.12 --port 27017 -u app -pList databases with their sizes
show dbsSwitch databases; a missing one is created on first write
use appdbShow collection count, document count and data size
db.stats()List collections in the current database
show collectionsInsert one document; an ObjectId is generated when _id is omitted
db.orders.insertOne({ userId: 42, status: 1, amount: 99.5, createdAt: new Date() })Bulk insert; ordered writes stop at the first failure by default
db.orders.insertMany([{ userId: 43, status: 1 }, { userId: 44, status: 1 }])Query with projection, sorting and a limit instead of returning whole documents
db.orders.find({ status: 1 }, { name: 1, amount: 1 }).sort({ _id: -1 }).limit(20)Update selected fields of one document and keep the rest untouched
db.orders.updateOne({ _id: ObjectId("<ID>") }, { $set: { status: 2 } })Update every matching document
db.orders.updateMany({ status: 0 }, { $set: { archived: true } })Delete matching documents; count them with find first
db.orders.deleteMany({ status: 0 })Count matching documents; more reliable than count on large collections
db.orders.countDocuments({ status: 1 })Create a compound index covering the common user-and-time sort
db.orders.createIndex({ userId: 1, createdAt: -1 })List all indexes on a collection
db.orders.getIndexes()Inspect documents scanned, timing and index usage
db.orders.find({ userId: 42 }).explain("executionStats")Drop an unused index to reduce write overhead
db.orders.dropIndex("userId_1_createdAt_-1")Aggregate total order amount per user
db.orders.aggregate([{ $match: { status: 1 } }, { $group: { _id: "$userId", total: { $sum: "$amount" } } }])Show replica set members, roles and replication lag
rs.status()Initialize a single-node replica set, common in test environments
rs.initiate({ _id: "rs0", members: [{ _id: 0, host: "10.0.0.12:27017" }] })Show replica set configuration including priority and voting
rs.conf()Show sharding status, shard keys and chunk distribution
sh.status()Shard a collection on a hashed key for even write distribution
sh.shardCollection("appdb.orders", { userId: "hashed" })Dump the database logically into the target directory
mongodump --uri="mongodb://app:<PASSWORD>@10.0.0.12:27017/appdb" --out=/backup/20261030Restore a dump directory; existing data is kept by default
mongorestore --uri="mongodb://app:<PASSWORD>@10.0.0.12:27017" /backup/20261030Export a collection to CSV for business review
mongoexport --uri="mongodb://app:<PASSWORD>@10.0.0.12:27017/appdb" --collection=orders --type=csv --fields=id,userId,status --out=/tmp/orders.csvExports contain data only, no indexes; use mongodump and mongorestore for restores
Lock writes for a consistent file-level snapshot; always follow with db.fsyncUnlock()
db.fsyncLock()Writes block while locked, so keep the window short and pair it with a filesystem snapshot
This website uses cookies to ensure you get the best experience on our website. Privacy Policy.