MongoDB Cheat Sheet
Quick reference for MongoDB queries, CRUD operations, aggregation pipeline, indexes, and mongosh commands. All essential MongoDB operations in one page.
Database & Collection
| show dbs | List all databases |
| use mydb | Switch to (or create) database |
| db.createCollection("users") | Create collection explicitly |
| show collections | List collections in current db |
| db.users.drop() | Delete a collection |
| db.dropDatabase() | Delete current database |
Insert (Create)
| db.users.insertOne({name: "Alice", age: 30}) | Insert one document |
| db.users.insertMany([{name: "Bob"}, {name: "Carol"}]) | Insert multiple documents |
| db.users.insertOne({_id: "custom-id", name: "Dave"}) | Insert with custom _id |
Find (Read)
| db.users.find() | Find all documents |
| db.users.find({age: 30}) | Find by exact value |
| db.users.findOne({name: "Alice"}) | Find first match |
| db.users.find({age: {$gt: 25}}) | Greater than ($gt, $gte, $lt, $lte) |
| db.users.find({name: {$in: ["Alice", "Bob"]}}) | Match any in array |
| db.users.find({$and: [{age: {$gt: 20}}, {age: {$lt: 40}}]}) | AND condition |
| db.users.find({$or: [{name: "Alice"}, {name: "Bob"}]}) | OR condition |
| db.users.find({}, {name: 1, _id: 0}) | Projection (select fields) |
| db.users.find().sort({age: -1}).limit(10) | Sort descending + limit |
| db.users.countDocuments({active: true}) | Count matching documents |
Update
| db.users.updateOne({name: "Alice"}, {$set: {age: 31}}) | Update one field |
| db.users.updateMany({active: false}, {$set: {archived: true}}) | Update many documents |
| db.users.updateOne({name: "Alice"}, {$inc: {age: 1}}) | Increment a value |
| db.users.updateOne({name: "Alice"}, {$push: {tags: "new"}}) | Push to array |
| db.users.updateOne({name: "Alice"}, {$pull: {tags: "old"}}) | Remove from array |
| db.users.updateOne({name: "Alice"}, {$unset: {age: ""}}) | Remove a field |
| db.users.replaceOne({name: "Alice"}, {name: "Alice", age: 32}) | Replace entire document |
Delete
| db.users.deleteOne({name: "Alice"}) | Delete first match |
| db.users.deleteMany({active: false}) | Delete all matches |
| db.users.deleteMany({}) | Delete all documents (keep collection) |
Aggregation Pipeline
| { $match: { status: "active" } } | Filter documents (like WHERE) |
| { $group: { _id: "$city", total: { $sum: 1 } } } | Group and aggregate |
| { $sort: { total: -1 } } | Sort results |
| { $project: { name: 1, upper: { $toUpper: "$name" } } } | Reshape documents |
| { $lookup: { from: "orders", localField: "_id", foreignField: "userId", as: "orders" } } | Join collections (LEFT JOIN) |
| { $unwind: "$tags" } | Flatten array field into documents |
| { $limit: 10 } | Limit output documents |
| { $skip: 20 } | Skip N documents (pagination) |
Indexes
| db.users.createIndex({email: 1}) | Single field index (ascending) |
| db.users.createIndex({email: 1}, {unique: true}) | Unique index |
| db.users.createIndex({name: 1, age: -1}) | Compound index |
| db.users.createIndex({bio: "text"}) | Text index (full-text search) |
| db.users.getIndexes() | List all indexes |
| db.users.dropIndex("email_1") | Drop an index by name |
Step-by-Step Guide
Read Guide →