$devvkit resources --cheatsheet mongodb-cheatsheet
MongoDB Cheatsheet
[databases]
MongoDB CRUD operations, aggregation pipeline, indexes, and query operators.
MongoDB is the leading NoSQL document database. Data is stored as BSON documents (similar to JSON) in collections. This cheatsheet covers CRUD operations, query operators, the aggregation pipeline, and indexing strategies.
All commands use the mongosh shell syntax.
Database & Collection Ops
$
use <db>Switch to or create a database.Example: use myApp
$
db.createCollection("name")Explicitly create a collection. Collections are created implicitly on first insert too.Example: db.createCollection("users")
$
show collectionsList all collections in the current database.Example: show collections
$
db.collection.drop()Drop an entire collection and its indexes.Example: db.temp_logs.drop()
CRUD Operations
$
db.collection.insertOne({ ... })Insert a single document. Returns insertedId.Example: db.users.insertOne({ name: "Alice", email: "alice@example.com" })
$
db.collection.insertMany([...])Insert multiple documents at once. Much faster than individual inserts.Example: db.users.insertMany([{ name: "Bob" }, { name: "Charlie" }])
$
db.collection.find({ ... })Query documents. Returns a cursor. Add .pretty() for formatted output.Example: db.users.find({ age: { $gt: 18 } }).pretty()
$
db.collection.findOne({ ... })Return the first document matching the query, or null.Example: db.users.findOne({ email: "alice@example.com" })
$
db.collection.updateOne(filter, update)Update the first document matching the filter.Example: db.users.updateOne({ name: "Alice" }, { $set: { age: 31 } })
$
db.collection.updateMany(filter, update)Update all documents matching the filter.Example: db.users.updateMany({ status: "inactive" }, { $set: { active: false } })
$
db.collection.deleteOne(filter)Delete the first document matching the filter.Example: db.users.deleteOne({ email: "spam@example.com" })
$
db.collection.deleteMany(filter)Delete all documents matching the filter.Example: db.users.deleteMany({ status: "deleted" })
Query Operators
$
{ $gt: val } / { $lt: val } / { $gte } / { $lte }Comparison operators: greater than, less than, etc.Example: db.orders.find({ total: { $gte: 100 } })
$
{ $in: [val1, val2] } / { $nin }Match any value in / not in an array.Example: db.users.find({ status: { $in: ["active", "pending"] } })
$
{ $exists: true }Match documents where a field exists (or not exists).Example: db.users.find({ deleted_at: { $exists: false } })
$
{ field: { $regex: "pattern", $options: "i" } }Regex search on a field. The i option makes it case-insensitive.Example: db.users.find({ name: { $regex: "^alice", $options: "i" } })
Aggregation Pipeline
$
db.collection.aggregate([ { $match: ... }, { $group: ... }, { $sort: ... } ])Aggregation pipeline — process documents through stages.Example: db.orders.aggregate([
{ $match: { status: "complete" } },
{ $group: { _id: "$category", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } }
])
$
{ $project: { field: 1, _id: 0 } }Reshape documents — include/exclude fields.Example: { $project: { name: 1, email: 1, _id: 0 } }
$
{ $unwind: "$arrayField" }Deconstruct an array field, creating a document for each element.Example: { $unwind: "$tags" }
$
{ $lookup: { from: "collection", localField: "x", foreignField: "_id", as: "results" } }Perform a left outer join with another collection.Example: { $lookup: { from: "orders", localField: "_id", foreignField: "user_id", as: "orders" } }
Indexes & Performance
$
db.collection.createIndex({ field: 1 })Create an ascending (1) or descending (-1) index on a field.Example: db.users.createIndex({ email: 1 }, { unique: true })
$
db.collection.createIndex({ field1: 1, field2: -1 })Compound index for queries filtering on multiple fields.Example: db.orders.createIndex({ user_id: 1, created_at: -1 })
$
db.collection.getIndexes()List all indexes on a collection.Example: db.users.getIndexes()
$
db.collection.explain("executionStats").find(...)Analyze query performance. Shows scanned docs, index usage, timing.Example: db.users.explain("executionStats").find({ email: "x" })
MongoDB Cheatsheet
MongoDB CRUD operations, aggregation pipeline, indexes, and query operators.
MongoDB is the leading NoSQL document database. Data is stored as BSON documents (similar to JSON) in collections. This cheatsheet covers CRUD operations, query operators, the aggregation pipeline, and indexing strategies.
All commands use the mongosh shell syntax.
Database & Collection Ops
use <db> — Switch to or create a database.Example: use myApp
db.createCollection("name") — Explicitly create a collection. Collections are created implicitly on first insert too.Example: db.createCollection("users")
show collections — List all collections in the current database.Example: show collections
db.collection.drop() — Drop an entire collection and its indexes.Example: db.temp_logs.drop()
CRUD Operations
db.collection.insertOne({ ... }) — Insert a single document. Returns insertedId.Example: db.users.insertOne({ name: "Alice", email: "alice@example.com" })
db.collection.insertMany([...]) — Insert multiple documents at once. Much faster than individual inserts.Example: db.users.insertMany([{ name: "Bob" }, { name: "Charlie" }])
db.collection.find({ ... }) — Query documents. Returns a cursor. Add .pretty() for formatted output.Example: db.users.find({ age: { $gt: 18 } }).pretty()
db.collection.findOne({ ... }) — Return the first document matching the query, or null.Example: db.users.findOne({ email: "alice@example.com" })
db.collection.updateOne(filter, update) — Update the first document matching the filter.Example: db.users.updateOne({ name: "Alice" }, { $set: { age: 31 } })
db.collection.updateMany(filter, update) — Update all documents matching the filter.Example: db.users.updateMany({ status: "inactive" }, { $set: { active: false } })
db.collection.deleteOne(filter) — Delete the first document matching the filter.Example: db.users.deleteOne({ email: "spam@example.com" })
db.collection.deleteMany(filter) — Delete all documents matching the filter.Example: db.users.deleteMany({ status: "deleted" })
Query Operators
{ $gt: val } / { $lt: val } / { $gte } / { $lte } — Comparison operators: greater than, less than, etc.Example: db.orders.find({ total: { $gte: 100 } })
{ $in: [val1, val2] } / { $nin } — Match any value in / not in an array.Example: db.users.find({ status: { $in: ["active", "pending"] } })
{ $exists: true } — Match documents where a field exists (or not exists).Example: db.users.find({ deleted_at: { $exists: false } })
{ field: { $regex: "pattern", $options: "i" } } — Regex search on a field. The i option makes it case-insensitive.Example: db.users.find({ name: { $regex: "^alice", $options: "i" } })
Aggregation Pipeline
db.collection.aggregate([ { $match: ... }, { $group: ... }, { $sort: ... } ]) — Aggregation pipeline — process documents through stages.Example: db.orders.aggregate([
{ $match: { status: "complete" } },
{ $group: { _id: "$category", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } }
])
{ $project: { field: 1, _id: 0 } } — Reshape documents — include/exclude fields.Example: { $project: { name: 1, email: 1, _id: 0 } }
{ $unwind: "$arrayField" } — Deconstruct an array field, creating a document for each element.Example: { $unwind: "$tags" }
{ $lookup: { from: "collection", localField: "x", foreignField: "_id", as: "results" } } — Perform a left outer join with another collection.Example: { $lookup: { from: "orders", localField: "_id", foreignField: "user_id", as: "orders" } }
Indexes & Performance
db.collection.createIndex({ field: 1 }) — Create an ascending (1) or descending (-1) index on a field.Example: db.users.createIndex({ email: 1 }, { unique: true })
db.collection.createIndex({ field1: 1, field2: -1 }) — Compound index for queries filtering on multiple fields.Example: db.orders.createIndex({ user_id: 1, created_at: -1 })
db.collection.getIndexes() — List all indexes on a collection.Example: db.users.getIndexes()
db.collection.explain("executionStats").find(...) — Analyze query performance. Shows scanned docs, index usage, timing.Example: db.users.explain("executionStats").find({ email: "x" })