Choosing between joins and embedding helps organize data efficiently in MongoDB. It affects how fast and easy it is to get related information.
Joins vs embedding decision in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
No fixed syntax because this is a design choice: - Embedding: store related data inside a document as nested objects or arrays. - Referencing (joins): store related data in separate collections and link by IDs.
Embedding means putting related data inside one document.
Referencing means linking documents using IDs and combining them with queries.
Examples
MongoDB
{
_id: 1,
name: "Alice",
address: {
street: "123 Main St",
city: "Townsville"
}
}MongoDB
{
_id: 1,
name: "Alice",
address_id: 101
}
// In another collection:
{
_id: 101,
street: "123 Main St",
city: "Townsville"
}Sample Program
This example shows embedding orders inside the user document. It is easy to get all orders with one query.
MongoDB
db.users.insertOne({
_id: 1,
name: "Bob",
orders: [
{ order_id: 1001, item: "Book", quantity: 1 },
{ order_id: 1002, item: "Pen", quantity: 3 }
]
})
// Query to find Bob's orders
const user = db.users.findOne({_id: 1})
user.ordersImportant Notes
Embedding is good for data that is mostly read together and does not grow too big.
Referencing is better when related data is large or shared among many documents.
MongoDB does not support traditional SQL joins, but you can use $lookup for referencing.
Summary
Embedding keeps related data inside one document for fast access.
Referencing links data in separate collections for flexibility and reuse.
Choose based on how your data is used and how often it changes.
Practice
1. Which scenario is best suited for embedding related data in MongoDB?
easy
Solution
Step 1: Understand embedding use case
Embedding stores related data inside one document for fast access and atomic updates.Step 2: Match scenario to embedding benefits
If data is accessed together and rarely changes, embedding avoids extra lookups and is efficient.Final Answer:
When related data is frequently accessed together and rarely changes -> Option BQuick Check:
Embedding = fast access, rare changes [OK]
Hint: Embed when data is read together and changes rarely [OK]
Common Mistakes:
- Embedding large, frequently changing data
- Embedding data shared across many documents
- Confusing embedding with referencing
2. Which of the following is the correct way to reference another document in MongoDB?
easy
Solution
Step 1: Identify referencing syntax
Referencing stores the ObjectId of another document to link collections.Step 2: Match correct reference format
Storing the ObjectId directly (e.g., user_id: ObjectId('abc123')) is the standard referencing method.Final Answer:
{ user_id: ObjectId('abc123') } inside the document -> Option CQuick Check:
Reference = store ObjectId [OK]
Hint: Reference by storing ObjectId, not embedding full data [OK]
Common Mistakes:
- Embedding full document instead of referencing
- Using deprecated $ref and $id fields
- Storing plain strings instead of ObjectId
3. Given two collections:
orders with embedded items array, what is the main benefit of embedding items inside orders?medium
Solution
Step 1: Understand embedding effect on queries
Embedding items inside orders means all item data is in one document.Step 2: Identify benefit of embedding items
This allows fetching an order and its items in a single query, improving speed.Final Answer:
Faster retrieval of all items for an order without extra queries -> Option AQuick Check:
Embedding = single query fetch [OK]
Hint: Embedding avoids extra queries for related data [OK]
Common Mistakes:
- Thinking embedding reduces document size
- Assuming embedded data can be reused easily
- Expecting automatic foreign key enforcement
4. You have a MongoDB schema where user profiles embed their addresses. You notice address updates are frequent and slow. What is the best fix?
medium
Solution
Step 1: Identify problem with embedding frequent updates
Embedding addresses means updating user documents often, which can be slow and large.Step 2: Choose solution for frequent changing data
Referencing addresses separately allows updating addresses independently without rewriting user documents.Final Answer:
Switch to referencing addresses in a separate collection -> Option AQuick Check:
Frequent updates = use referencing [OK]
Hint: Use referencing for frequently updated data [OK]
Common Mistakes:
- Adding indexes without fixing schema design
- Embedding more fields increases document size
- Increasing document size limit doesn't improve update speed
5. You design a blogging platform where posts have comments. Comments can be many and users want to edit them independently. Which design is best?
hard
Solution
Step 1: Analyze comment characteristics
Comments can be many and need independent editing, so they change often and grow large.Step 2: Choose schema design for many, editable comments
Referencing comments in a separate collection allows independent updates and avoids large post documents.Final Answer:
Store comments in a separate collection and reference post ID -> Option DQuick Check:
Many editable items = referencing best [OK]
Hint: Many changing items = use referencing, not embedding [OK]
Common Mistakes:
- Embedding many comments causes large documents
- Embedding only latest comment complicates queries
- Storing comments as plain text fields loses structure
