MongoDB Cheat Codes
MongoDB Cheat Codes for document CRUD, aggregation pipelines, $lookup joins, and indexing. 20 essential cheat codes ready to copy and execute.
Query documents matching filter criteria.
db.collection.find({ key: "value" }); db.users.find({ status: "active" }); Retrieve single document matching filter.
db.collection.findOne({ _id: ObjectId("...") }); db.users.findOne({ email: "alex@dev.com" }); Insert single document into collection.
db.collection.insertOne({ key: "val" }); db.orders.insertOne({ userId: 10, total: 99.99, createdAt: new Date() }); Insert multiple documents in bulk.
db.collection.insertMany([ { doc1 }, { doc2 } ]); db.tags.insertMany([ { name: "sql" }, { name: "nosql" } ]); Modify fields in single matching document.
db.collection.updateOne({ filter }, { $set: { key: "val" } }); db.users.updateOne({ _id: 1 }, { $set: { status: "premium" } }); Increment numeric field across multiple documents.
db.collection.updateMany({ filter }, { $inc: { count: 1 } }); db.stats.updateMany({ active: true }, { $inc: { views: 1 } }); Remove single document from collection.
db.collection.deleteOne({ _id: val }); db.sessions.deleteOne({ _id: "sess_123" }); Remove all documents matching filter.
db.collection.deleteMany({ filter }); db.logs.deleteMany({ level: "debug" }); Multi-stage pipeline filter and group totals.
db.collection.aggregate([ { $match: ... }, { $group: ... } ]); db.orders.aggregate([ { $match: { status: "completed" } }, { $group: { _id: "$userId", total: { $sum: "$amount" } } } ]); Left outer join secondary collection.
db.collection.aggregate([ { $lookup: { from: "col", localField: "a", foreignField: "b", as: "out" } } ]); db.orders.aggregate([ { $lookup: { from: "users", localField: "userId", foreignField: "_id", as: "user" } } ]); Deconstruct array field into document rows.
db.collection.aggregate([ { $unwind: "$arrayField" } ]); db.articles.aggregate([ { $unwind: "$tags" } ]); Build spatial index for geospatial queries.
db.collection.createIndex({ location: "2dsphere" }); db.places.createIndex({ location: "2dsphere" }); Build text search index over document fields.
db.collection.createIndex({ field: "text" }); db.posts.createIndex({ title: "text", content: "text" }); Set automatic document deletion TTL timestamp.
db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 }); db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 86400 }); Remove specified index from collection.
db.collection.dropIndex("index_name"); db.users.dropIndex("email_1"); Count total documents matching query filter.
db.collection.countDocuments({ filter }); db.users.countDocuments({ role: "admin" }); Paginate document search query output.
db.collection.find().sort({ field: -1 }).skip(20).limit(10); db.products.find().sort({ price: 1 }).skip(0).limit(10); Insert document if no update match found.
db.collection.updateOne({ filter }, { $set: ... }, { upsert: true }); db.stats.updateOne({ date: "2026-01-01" }, { $inc: { views: 1 } }, { upsert: true }); Enforce strict JSON Schema validation rules.
db.createCollection("name", { validator: { $jsonSchema: ... } }); db.createCollection("users", { validator: { $jsonSchema: { required: ["email"] } } }); Inspect query execution statistics and index usage.
db.collection.find({ filter }).explain("executionStats"); db.orders.find({ status: "pending" }).explain("executionStats"); No cheat codes found matching your search term.