Embedding and referencing help organize related data in MongoDB. Choosing between them affects how easy and fast it is to get your data.
Embedding vs referencing decision in MongoDB
Embedding example:
{
_id: ObjectId("..."),
name: "Alice",
address: {
street: "123 Main St",
city: "Townsville"
}
}
Referencing example:
{
_id: ObjectId("..."),
name: "Alice",
address_id: ObjectId("...")
}
Address document:
{
_id: ObjectId("..."),
street: "123 Main St",
city: "Townsville"
}Embedding stores related data inside the same document.
Referencing stores related data in separate documents and links them by ID.
{
_id: 1,
name: "Book",
author: {
name: "John Doe",
age: 45
}
}{
_id: 1,
name: "Book",
author_id: 101
}
{
_id: 101,
name: "John Doe",
age: 45
}This example shows embedding author info inside a book document. When we get the book, we get author details too.
db.books.insertOne({
_id: 1,
title: "MongoDB Guide",
author: {
name: "Jane Smith",
email: "jane@example.com"
}
})
const book = db.books.findOne({_id: 1})
printjson(book)Embedding is good for data that belongs together and changes together.
Referencing is better when data is large, shared, or changes independently.
Think about how you will read and update your data to choose the best method.
Embedding stores related data inside one document for fast access.
Referencing links documents by ID to keep data separate and reusable.
Choose embedding for small, tightly related data; choose referencing for large or shared data.
Practice
Solution
Step 1: Understand embedding use case
Embedding is best when related data is small and tightly coupled, like a user's addresses.Step 2: Analyze options for embedding
Storing a user's profile and their list of addresses when addresses are few and rarely shared fits because addresses are few and not shared, so embedding improves read speed.Final Answer:
Storing a user's profile and their list of addresses when addresses are few and rarely shared. -> Option AQuick Check:
Embedding = small, tightly related data [OK]
- Embedding large or shared data causing duplication
- Referencing small, simple data unnecessarily
Solution
Step 1: Identify referencing syntax
Referencing stores the related document's ID as a field, usually named with _id suffix.Step 2: Check options for correct ID reference
{ "author_id": ObjectId("507f1f77bcf86cd799439011") } correctly stores the ObjectId with a clear field name author_id.Final Answer:
{ "author_id": ObjectId("507f1f77bcf86cd799439011") } -> Option AQuick Check:
Reference = store related document's ID [OK]
- Using string instead of ObjectId for references
- Using $ref and $id without DBRef context
- Embedding full document instead of referencing
orders where each order embeds a small array of items, what is the expected result of querying an order by ID?Solution
Step 1: Understand embedding query behavior
When data is embedded, querying the parent document returns all embedded data inside it.Step 2: Analyze query result for embedded items
Querying by order ID returns the order document including the embedded items array.Final Answer:
The order document with all embedded items included in the result. -> Option CQuick Check:
Embedding returns full embedded data [OK]
- Expecting separate documents for embedded data
- Thinking embedded arrays cause query errors
- Confusing referencing with embedding results
Solution
Step 1: Identify embedding impact on updates
Embedding large or frequently updated arrays causes the whole document to be rewritten, slowing updates.Step 2: Choose referencing to improve update performance
Referencing posts separately allows updating posts without rewriting the entire user document.Final Answer:
Embedding large arrays causes slow updates; switch to referencing posts separately. -> Option DQuick Check:
Large/frequent updates = use referencing [OK]
- Embedding large arrays despite frequent updates
- Ignoring update performance impact
- Misunderstanding ObjectId role in updates
Solution
Step 1: Analyze comment characteristics
Comments are numerous, large, and updated independently, so embedding would cause large documents and slow updates.Step 2: Choose referencing for scalability and independent updates
Referencing comments in a separate collection allows efficient updates and avoids large blog post documents.Final Answer:
Reference comments in a separate collection linked by post ID. -> Option BQuick Check:
Large, independent data = referencing [OK]
- Embedding large, frequently updated arrays
- Mixing embedding and referencing without clear rules
- Storing JSON strings instead of structured data
Ready to prove it?
Pass the test and this pattern moves unknown
