MongoDB
MongoDB cheat sheet with CRUD operations, query operators, aggregation pipelines, indexing, and schema design patterns with examples.
Sign in to mark items as known and track your progress.
Sign inCRUD Operations
Create, Read, Update, and Delete documents in MongoDB
Insert Documents
Add new documents to a collection
// Insert one document
db.users.insertOne({
name: "John Doe",
email: "john@example.com",
age: 30,
created: new Date()
})
// Insert multiple documents
db.users.insertMany([
{ name: "Alice", age: 25 },
{ name: "Bob", age: 35 }
])Find Documents
Query and retrieve documents from collections
// Find all documents
db.users.find()
// Find with filter
db.users.findOne({ email: "john@example.com" })
// Find multiple with conditions
db.users.find({
age: { $gte: 18 },
status: "active"
})
// Projection (select fields)
db.users.find(
{ age: { $gte: 21 } },
{ name: 1, email: 1, _id: 0 }
)Update Documents
Modify existing documents in collections
// Update one document
db.users.updateOne(
{ email: "john@example.com" },
{ $set: { age: 31, modified: new Date() } }
)
// Update multiple documents
db.users.updateMany(
{ status: "inactive" },
{ $set: { status: "archived" } }
)
// Update or insert (upsert)
db.users.updateOne(
{ email: "new@example.com" },
{ $set: { name: "New User", age: 25 } },
{ upsert: true }
)Delete Documents
Remove documents from collections
// Delete one document
db.users.deleteOne({ email: "john@example.com" })
// Delete multiple documents
db.users.deleteMany({ status: "archived" })
// Delete all documents (careful!)
db.users.deleteMany({})Query Operators
Powerful operators for filtering and matching documents
Comparison Operators
Compare field values in queries
// Equality
db.products.find({ price: 29.99 })
db.products.find({ category: { $eq: "electronics" } })
// Inequality
db.products.find({ price: { $ne: 0 } })
// Greater/Less than
db.products.find({
price: { $gt: 20, $lte: 100 } // 20 < price <= 100
})
// In/Not in array
db.products.find({
category: { $in: ["electronics", "books"] }
})
db.products.find({
status: { $nin: ["deleted", "archived"] }
})Logical Operators
Combine multiple query conditions
// AND - all conditions must match
db.users.find({
$and: [
{ age: { $gte: 18 } },
{ status: "active" },
{ emailVerified: true }
]
})
// OR - any condition can match
db.products.find({
$or: [
{ category: "sale" },
{ price: { $lt: 20 } },
{ featured: true }
]
})
// NOT - invert condition
db.users.find({
status: { $not: { $eq: "banned" } }
})
// NOR - none of the conditions
db.products.find({
$nor: [
{ discontinued: true },
{ stock: 0 }
]
})Array Operators
Query and match array fields
// Match array containing value
db.posts.find({ tags: "mongodb" })
// Match array with all values
db.posts.find({
tags: { $all: ["mongodb", "database"] }
})
// Match array by size
db.users.find({
hobbies: { $size: 3 }
})
// Match array element
db.orders.find({
"items.product": "laptop"
})Text Search
Full-text search capabilities
// Create text index first
db.articles.createIndex({
title: "text",
content: "text"
})
// Basic text search
db.articles.find({
$text: { $search: "mongodb tutorial" }
})
// Exact phrase search
db.articles.find({
$text: { $search: '"exact phrase"' }
})
// Exclude terms
db.articles.find({
$text: { $search: 'mongodb -sql' }
})Update Operators
Operators for modifying document fields and arrays
Field Update Operators
Modify document field values
// Set field values
db.users.updateOne(
{ _id: userId },
{
$set: { name: "John", "address.city": "NYC" },
$unset: { tempField: "" }
}
)
// Increment/Decrement
db.products.updateOne(
{ _id: productId },
{
$inc: {
views: 1,
stock: -5
}
}
)
// Multiply value
db.items.updateOne(
{ _id: itemId },
{ $mul: { price: 1.1 } } // 10% increase
)Array Update Operators
Modify array fields in documents
// Add to array
db.users.updateOne(
{ _id: userId },
{
$push: { tags: "premium" },
$addToSet: { hobbies: "reading" } // No duplicates
}
)
// Add multiple items
db.posts.updateOne(
{ _id: postId },
{
$push: {
comments: {
$each: [comment1, comment2],
$sort: { date: -1 },
$slice: 10 // Keep only 10 newest
}
}
}
)
// Remove from array
db.users.updateOne(
{ _id: userId },
{
$pull: { tags: "old" },
$pop: { history: -1 } // Remove first (-1) or last (1)
}
)Aggregation Pipeline
Transform and analyze data with pipeline stages
Basic Pipeline Stages
Common stages for data transformation
// Basic aggregation pipeline
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: {
_id: "$customerId",
totalSpent: { $sum: "$total" },
orderCount: { $sum: 1 }
}},
{ $sort: { totalSpent: -1 } },
{ $limit: 10 }
])
// Project (reshape) documents
db.users.aggregate([
{ $project: {
name: 1,
email: 1,
fullName: { $concat: ["$firstName", " ", "$lastName"] },
age: { $subtract: [
{ $year: new Date() },
{ $year: "$birthDate" }
]}
}}
])Aggregation Operators
Operators for calculations and transformations
// Group accumulator operators
db.sales.aggregate([
{ $group: {
_id: "$category",
total: { $sum: "$amount" },
average: { $avg: "$amount" },
min: { $min: "$amount" },
max: { $max: "$amount" },
count: { $sum: 1 },
items: { $push: "$item" },
uniqueItems: { $addToSet: "$item" }
}}
])
// String operators
{ $project: {
upper: { $toUpper: "$name" },
lower: { $toLower: "$name" },
substring: { $substr: ["$name", 0, 3] },
concat: { $concat: ["$first", " ", "$last"] }
}}Indexes & Performance
Optimize query performance with proper indexing
Index Types
Different index types for various use cases
// Single field index
db.users.createIndex({ email: 1 }) // 1 = ascending
db.posts.createIndex({ createdAt: -1 }) // -1 = descending
// Compound index (multiple fields)
db.orders.createIndex({
customerId: 1,
createdAt: -1
})
// Unique index
db.users.createIndex(
{ email: 1 },
{ unique: true }
)
// Text index for search
db.articles.createIndex({
title: "text",
content: "text"
})Query Optimization
Analyze and optimize query performance
// Explain query execution
db.users.find({
age: { $gte: 18 }
}).explain("executionStats")
// Check index usage
db.users.find({ email: "john@example.com" })
.hint({ email: 1 }) // Force specific index
.explain()
// Get index statistics
db.users.getIndexes()
db.users.totalIndexSize()Schema Design & Validation
Design patterns and data validation strategies
Schema Validation
Enforce document structure and data types
// Create collection with validation
db.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "email", "age"],
properties: {
name: {
bsonType: "string",
description: "must be a string"
},
email: {
bsonType: "string",
pattern: "^[a-z0-9+_.-]+@[a-z0-9.-]+$"
},
age: {
bsonType: "int",
minimum: 18,
maximum: 120
}
}
}
}
})Design Patterns
Common MongoDB schema design patterns
// Embedding pattern (denormalization)
{
_id: ObjectId("..."),
name: "John Doe",
addresses: [
{ type: "home", street: "123 Main St", city: "NYC" },
{ type: "work", street: "456 Corp Ave", city: "NYC" }
]
}
// Reference pattern (normalization)
// Users collection
{ _id: userId, name: "John", orderIds: [order1, order2] }
// Orders collection
{ _id: order1, customerId: userId, total: 99.99 }
// Hybrid pattern
{
_id: postId,
title: "MongoDB Tips",
author: { // Embed frequently needed data
_id: authorId,
name: "Jane Doe",
avatar: "jane.jpg"
},
// Reference for full author details
authorId: authorId
}