MongoDB & Mongosh Queries Cheat Sheet
Production MongoDB command cheat sheet: find filters ($in, $gte), projections, updateOne ($set), aggregation pipelines ($match, $group), and unique indexes with 1-click copy.
Database & Shell (5 commands)
mongosh "mongodb://localhost:27017/mydb"
Connects to a local MongoDB instance using the modern MongoDB Shell (mongosh).
show dbs
Lists all databases and their disk storage sizes on the connected cluster.
use <database_name>
Switches the current active database context. Creates database upon first insert.
show collections
Displays all collections (tables) present within the currently selected database.
db.stats()
Displays storage statistics, index sizes, data size, and document count for the database.
CRUD: Read Queries (5 commands)
db.users.find()
Retrieves all documents from the users collection with default batch pagination.
db.users.find({ status: "active" }, { name: 1, email: 1, _id: 0 })
Queries documents with projection, returning only name and email fields without _id.
db.users.find({ age: { $gte: 21, $lte: 65 } })
Filters documents using range comparison operators ($gte, $lte, $gt, $lt, $ne).
db.users.find({ role: { $in: ["admin", "editor"] } })
Matches documents where the field value matches any element in the specified array.
db.users.find({ "address.city": "San Francisco" })
Queries nested sub-documents and embedded objects using dot notation.
CRUD: Write & Update (5 commands)
db.users.insertOne({ name: "Alice", email: "[email protected]", createdAt: new Date() })
Inserts a single document and returns the newly generated ObjectId _id.
db.users.updateOne({ _id: ObjectId("...") }, { $set: { status: "verified" } })
Updates specified fields in a single document matching the filter criteria.
db.users.updateMany({ active: false }, { $set: { archived: true } })
Updates all documents matching the query filter in a single atomic operation.
db.users.updateOne({ email: "[email protected]" }, { $set: { loginCount: 1 } }, { upsert: true })
Performs an UPSERT: inserts a new document if no matching record is found.
db.users.deleteOne({ _id: ObjectId("...") })
Deletes a single document matching the specified criteria.
Indexes & Profiling (4 commands)
db.users.createIndex({ email: 1 }, { unique: true })
Creates an ascending single-field unique index enforcing zero duplicate emails.
db.orders.createIndex({ customerId: 1, createdAt: -1 })
Creates a compound index supporting queries filtered by customer and sorted by date.
db.users.getIndexes()
Lists all indexes configured on the collection, including sizes and build versions.
db.users.find({ email: "[email protected]" }).explain("executionStats")
Analyzes query execution plans, index scans (IXSCAN) vs collection scans (COLLSCAN).
Aggregations & Backup (2 commands)
db.orders.aggregate([{ $match: { status: "completed" } }, { $group: { _id: "$customerId", total: { $sum: "$amount" } } }])
Executes an aggregation pipeline filtering orders and computing total revenue per user.
mongodump --uri="mongodb://localhost:27017/mydb" --out=/backup/$(date +%F)
Creates a BSON dump archive of the database for snapshot backups.