MongoDB Reference
Free reference guide: MongoDB Reference
About MongoDB Reference
The MongoDB Reference is a comprehensive, searchable guide covering all essential MongoDB operations organized into eight categories: CRUD (insertOne, insertMany, find, findOne, updateOne, updateMany, deleteOne, deleteMany, replaceOne), Aggregation Pipeline ($match, $group, $sort, $project, $lookup, $unwind), Indexes (createIndex, compound indexes, text indexes, TTL indexes), Schema Validation ($jsonSchema, validationLevel, embedded documents, reference pattern), Replication (rs.initiate, rs.status, rs.add, rs.stepDown, Read Preference), Sharding (sh.enableSharding, sh.shardCollection, range and hash sharding), Security (createUser, role-based access, TLS/SSL, audit logging), and Admin (mongodump, mongorestore, db.stats, db.currentOp, explain).
Backend developers, data engineers, and database administrators use MongoDB as a flexible document database for applications ranging from e-commerce catalogs and user profiles to IoT sensor data and content management systems. MongoDB's document model stores data as BSON (Binary JSON), allowing nested objects and arrays without rigid schema enforcement. The aggregation pipeline provides powerful data transformation capabilities — $lookup enables cross-collection joins, $group computes grouped totals and counts, and $project reshapes output documents.
This reference is particularly useful when constructing complex aggregation pipelines, choosing between embedded document and reference patterns for schema design, setting up replica sets for high availability with automatic failover, configuring sharding for horizontal scaling across multiple servers, and using EXPLAIN to analyze query execution plans. Each entry includes a complete code example showing realistic usage with collections like users, orders, and sessions.
Key Features
- Eight categories: CRUD, Aggregation, Indexes, Schema, Replication, Sharding, Security, Admin
- Full CRUD operations: insertOne/insertMany, find/findOne, updateOne/updateMany ($set/$inc), deleteOne/deleteMany, replaceOne
- Aggregation pipeline stages: $match, $group ($sum), $sort, $project ($toUpper), $lookup (cross-collection join), $unwind
- Index types: single-field (unique), compound (multi-field), text (full-text search), TTL (auto-expiry), with getIndexes/dropIndex
- Schema validation with $jsonSchema: bsonType, required fields, property constraints, and validationLevel/validationAction
- Replica set setup: rs.initiate() with multi-member config, rs.status(), rs.add(), rs.stepDown(), read preference
- Sharding: sh.enableSharding(), sh.shardCollection(), range-based vs hash-based shard key strategies
- Admin tools: mongodump/mongorestore for backup/restore, db.currentOp() for live query monitoring, explain("executionStats")
Frequently Asked Questions
What is the difference between insertOne() and insertMany() in MongoDB?
insertOne() inserts a single document and returns the _id of the inserted document. insertMany() accepts an array of documents and inserts them all in a single operation, returning an array of inserted IDs. For bulk imports, insertMany() is significantly faster than calling insertOne() in a loop because it batches the writes into fewer network round trips.
How does the MongoDB aggregation pipeline work?
The aggregation pipeline is a sequence of stages where each stage transforms the input documents and passes the result to the next stage. Common stages include $match (filter documents like WHERE in SQL), $group (aggregate values like GROUP BY), $sort (order results), $project (reshape fields like SELECT), $lookup (join with another collection), and $unwind (flatten arrays into separate documents).
When should I use $lookup in an aggregation pipeline?
$lookup performs a left outer join between two collections. For example, joining orders with users: { $lookup: { from: "users", localField: "userId", foreignField: "_id", as: "user" } }. Use $lookup when your data uses the reference pattern (storing ObjectId references) and you need to combine data from multiple collections in a single query result.
What is a TTL index in MongoDB and how do I create one?
A TTL (Time-To-Live) index automatically deletes documents after a specified number of seconds. Create one on a Date field: db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 }). This deletes session documents one hour after their createdAt timestamp. TTL indexes are commonly used for session management, cache entries, and log rotation.
What is the difference between embedded documents and reference patterns in MongoDB?
The embedded document pattern stores related data inside the same document (e.g., address nested inside a user document). This makes reads fast since everything is retrieved in one query. The reference pattern stores a separate document and links them with an ObjectId, similar to a foreign key. Use embedding for one-to-one or one-to-few relationships that are always read together; use references for one-to-many relationships or when data is shared across multiple parent documents.
How do I set up a MongoDB replica set?
Run rs.initiate() with a configuration object specifying the set name and member hosts: rs.initiate({ _id: "myReplicaSet", members: [{ _id: 0, host: "mongo1:27017" }, { _id: 1, host: "mongo2:27017" }, { _id: 2, host: "mongo3:27017" }] }). MongoDB elects one member as primary and the others as secondaries. Check status with rs.status(). Applications connect using a connection string listing all replica set members.
What is the difference between range sharding and hash sharding?
Range sharding distributes documents across shards based on value ranges of the shard key — for example, timestamps so that recent data lands on specific shards. This is efficient for range queries but can create hotspots if data is written sequentially. Hash sharding hashes the shard key value to distribute documents more uniformly across shards, preventing write hotspots. Use hash sharding for randomly distributed keys like user IDs.
How do I use explain() to analyze a slow MongoDB query?
Run db.collection.find(query).explain("executionStats") to see how MongoDB executes the query. Look at the "executionStats" section: "totalKeysExamined" shows how many index keys were scanned, "totalDocsExamined" shows how many documents were loaded, and "nReturned" shows how many were returned. If totalDocsExamined is much larger than nReturned, the query lacks an efficient index. The "winningPlan" section shows whether an index scan (IXSCAN) or full collection scan (COLLSCAN) was used.