MongoDB Cheat Sheet
MongoDB CRUD operations, aggregation pipeline, and indexing strategies.
2 PagesBeginnerApr 24, 2026
CRUD Operations
Create, read, update, delete documents.
javascript
db.users.insertOne({ name: "Alice", age: 30 });db.users.find({ age: { $gt: 18 } });db.users.updateOne({ name: "Alice" }, { $set: { age: 31 } });db.users.deleteOne({ name: "Alice" });
Query Operators
Common comparison and logical operators.
javascript
db.users.find({ age: { $gte: 18, $lte: 65 } });db.users.find({ $or: [{ role: "admin" }, { role: "editor" }] });db.users.find({ tags: { $in: ["vip", "beta"] } });
Aggregation Pipeline
Transform and summarize documents.
javascript
db.orders.aggregate([ { $match: { status: "paid" } }, { $group: { _id: "$customerId", total: { $sum: "$amount" } } }, { $sort: { total: -1 } },]);
Indexing
Speed up queries with indexes.
javascript
db.users.createIndex({ email: 1 }, { unique: true });db.users.getIndexes();
Update Operators
Modify documents in place without replacing them.
javascript
// Set / unset fieldsdb.users.updateOne({ _id: 1 }, { $set: { status: "active" } })db.users.updateOne({ _id: 1 }, { $unset: { temp: "" } })// Increment, multiply, renamedb.users.updateOne({ _id: 1 }, { $inc: { logins: 1 } })db.users.updateOne({ _id: 1 }, { $rename: { name: "fullName" } })// Array updatesdb.users.updateOne({ _id: 1 }, { $push: { tags: "vip" } })db.users.updateOne({ _id: 1 }, { $addToSet: { tags: "vip" } })db.users.updateOne({ _id: 1 }, { $pull: { tags: "old" } })// Upsert: insert if no matchdb.users.updateOne({ email: "[email protected]" }, { $set: { seen: true } }, { upsert: true })
Bulk Write & Ordered Ops
Batch multiple write operations in one round trip.
javascript
db.orders.bulkWrite([ { insertOne: { document: { _id: 1, total: 50 } } }, { updateOne: { filter: { _id: 2 }, update: { $set: { total: 75 } }, upsert: true } }, { deleteOne: { filter: { _id: 3 } } }], { ordered: false })// ordered:false keeps going after an error and can run in parallel
Multi-Document Transactions
Atomic operations across collections on a replica set.
javascript
const session = db.getMongo().startSession()session.startTransaction()try { const acct = session.getDatabase("bank").accounts acct.updateOne({ _id: "a" }, { $inc: { bal: -100 } }) acct.updateOne({ _id: "b" }, { $inc: { bal: 100 } }) session.commitTransaction()} catch (e) { session.abortTransaction() throw e} finally { session.endSession()}
More Aggregation Stages
Pipeline stages beyond match and group.
- $lookup- left outer join to another collection
- $unwind- deconstruct an array field into one document per element
- $project- include, exclude, or compute new fields
- $facet- run multiple sub-pipelines on the same input in one stage
- $bucket- group documents into ranges (histogram-style)
- $out / $merge- write pipeline results to a collection
- $sample- randomly select N documents
Pro Tip
Index the fields you query and sort on most often — use .explain("executionStats") to verify a query is actually using one.
Was this cheat sheet helpful?
Explore Topics
#MongoDB#MongoDBCheatSheet#Database#Beginner#CRUDOperations#QueryOperators#AggregationPipeline#Indexing#Databases#DevOps#CheatSheet#SkillVeris