SQLancer
← All Cheat Codes Library / Cheat Codes / MongoDB
🍃 MongoDB Cheat Codes

MongoDB Cheat Codes

MongoDB Cheat Codes for document CRUD, aggregation pipelines, $lookup joins, and indexing. 20 essential cheat codes ready to copy and execute.

# 1 find() CRUD

Query documents matching filter criteria.

Syntax:
db.collection.find({ key: "value" });
Working Example:
db.users.find({ status: "active" });
# 2 findOne() CRUD

Retrieve single document matching filter.

Syntax:
db.collection.findOne({ _id: ObjectId("...") });
Working Example:
db.users.findOne({ email: "alex@dev.com" });
# 3 insertOne() CRUD

Insert single document into collection.

Syntax:
db.collection.insertOne({ key: "val" });
Working Example:
db.orders.insertOne({ userId: 10, total: 99.99, createdAt: new Date() });
# 4 insertMany() CRUD

Insert multiple documents in bulk.

Syntax:
db.collection.insertMany([ { doc1 }, { doc2 } ]);
Working Example:
db.tags.insertMany([ { name: "sql" }, { name: "nosql" } ]);
# 5 updateOne() ($set) CRUD

Modify fields in single matching document.

Syntax:
db.collection.updateOne({ filter }, { $set: { key: "val" } });
Working Example:
db.users.updateOne({ _id: 1 }, { $set: { status: "premium" } });
# 6 updateMany() ($inc) CRUD

Increment numeric field across multiple documents.

Syntax:
db.collection.updateMany({ filter }, { $inc: { count: 1 } });
Working Example:
db.stats.updateMany({ active: true }, { $inc: { views: 1 } });
# 7 deleteOne() CRUD

Remove single document from collection.

Syntax:
db.collection.deleteOne({ _id: val });
Working Example:
db.sessions.deleteOne({ _id: "sess_123" });
# 8 deleteMany() CRUD

Remove all documents matching filter.

Syntax:
db.collection.deleteMany({ filter });
Working Example:
db.logs.deleteMany({ level: "debug" });
# 9 aggregate ($match & $group) Aggregation

Multi-stage pipeline filter and group totals.

Syntax:
db.collection.aggregate([ { $match: ... }, { $group: ... } ]);
Working Example:
db.orders.aggregate([ { $match: { status: "completed" } }, { $group: { _id: "$userId", total: { $sum: "$amount" } } } ]);
# 10 aggregate ($lookup) Aggregation

Left outer join secondary collection.

Syntax:
db.collection.aggregate([ { $lookup: { from: "col", localField: "a", foreignField: "b", as: "out" } } ]);
Working Example:
db.orders.aggregate([ { $lookup: { from: "users", localField: "userId", foreignField: "_id", as: "user" } } ]);
# 11 aggregate ($unwind) Aggregation

Deconstruct array field into document rows.

Syntax:
db.collection.aggregate([ { $unwind: "$arrayField" } ]);
Working Example:
db.articles.aggregate([ { $unwind: "$tags" } ]);
# 12 createIndex (2dsphere) Indexing

Build spatial index for geospatial queries.

Syntax:
db.collection.createIndex({ location: "2dsphere" });
Working Example:
db.places.createIndex({ location: "2dsphere" });
# 13 createIndex (Text) Indexing

Build text search index over document fields.

Syntax:
db.collection.createIndex({ field: "text" });
Working Example:
db.posts.createIndex({ title: "text", content: "text" });
# 14 createIndex (TTL Expire) Indexing

Set automatic document deletion TTL timestamp.

Syntax:
db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 });
Working Example:
db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 86400 });
# 15 dropIndex() Indexing

Remove specified index from collection.

Syntax:
db.collection.dropIndex("index_name");
Working Example:
db.users.dropIndex("email_1");
# 16 countDocuments() CRUD

Count total documents matching query filter.

Syntax:
db.collection.countDocuments({ filter });
Working Example:
db.users.countDocuments({ role: "admin" });
# 17 sort() & limit() & skip() Pagination

Paginate document search query output.

Syntax:
db.collection.find().sort({ field: -1 }).skip(20).limit(10);
Working Example:
db.products.find().sort({ price: 1 }).skip(0).limit(10);
# 18 upsert: true CRUD

Insert document if no update match found.

Syntax:
db.collection.updateOne({ filter }, { $set: ... }, { upsert: true });
Working Example:
db.stats.updateOne({ date: "2026-01-01" }, { $inc: { views: 1 } }, { upsert: true });
# 19 jsonSchema Validator Schema

Enforce strict JSON Schema validation rules.

Syntax:
db.createCollection("name", { validator: { $jsonSchema: ... } });
Working Example:
db.createCollection("users", { validator: { $jsonSchema: { required: ["email"] } } });
# 20 explain("executionStats") Performance

Inspect query execution statistics and index usage.

Syntax:
db.collection.find({ filter }).explain("executionStats");
Working Example:
db.orders.find({ status: "pending" }).explain("executionStats");