0
0
MongoDBquery~10 mins

Joins vs embedding decision in MongoDB - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Joins vs embedding decision
Start: Need related data
Decide data relation type
One-to-few
Embed data
Single doc
Query data accordingly
Decide if related data fits inside one document (embed) or needs separate documents linked by references (join). Then query accordingly.
Execution Sample
MongoDB
db.orders.aggregate([
  { $lookup: {
      from: 'products',
      localField: 'product_id',
      foreignField: '_id',
      as: 'product_info'
  }}
])
This query joins orders with products using $lookup to get product details inside each order.
Execution Table
StepActionInput DocumentLookup ResultOutput Document
1Read order document{_id: 1, product_id: 101, qty: 2}-{_id: 1, product_id: 101, qty: 2}
2Perform $lookup to find productproduct_id=101{_id: 101, name: 'Pen', price: 1.5}-
3Add product info to order--{_id: 1, product_id: 101, qty: 2, product_info: [{_id: 101, name: 'Pen', price: 1.5}]}
4Return enriched order document--{_id: 1, product_id: 101, qty: 2, product_info: [{_id: 101, name: 'Pen', price: 1.5}]}
5End of aggregation---
💡 All orders processed with product info joined using $lookup.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
orderDocnull{_id:1, product_id:101, qty:2}{_id:1, product_id:101, qty:2}{_id:1, product_id:101, qty:2, product_info:[{_id:101, name:'Pen', price:1.5}]}{_id:1, product_id:101, qty:2, product_info:[{_id:101, name:'Pen', price:1.5}]}
productInfonullnull{_id:101, name:'Pen', price:1.5}{_id:101, name:'Pen', price:1.5}{_id:101, name:'Pen', price:1.5}
Key Moments - 2 Insights
Why do we use $lookup instead of embedding product info directly?
Because product info can change or be large, embedding duplicates data and makes updates hard. $lookup joins data at query time, keeping data normalized. See execution_table step 2 where $lookup fetches product info.
When should we embed data instead of using $lookup?
Embed when related data is small and tightly connected, like one-to-few relationships, so reading one document gets all info without extra queries. This avoids $lookup overhead. The concept_flow shows embedding for one-to-few.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the output document contain?
AOnly order fields without product info
BOnly product details without order fields
COrder fields plus product_info array with product details
DEmpty document
💡 Hint
Check the Output Document column at step 3 in execution_table.
At which step does the $lookup operation fetch product data?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the Action column in execution_table where $lookup is mentioned.
If product info was embedded in orders, how would the execution_table change?
ANo $lookup step needed, product info present from start
BMore $lookup steps needed
COutput documents would be empty
DExecution would stop earlier
💡 Hint
Refer to concept_flow where embedding avoids joins.
Concept Snapshot
Joins vs Embedding Decision in MongoDB:
- Embed data when related info is small and tightly linked (one-to-few).
- Use references and $lookup for large or many related items (one-to-many).
- Embedding improves read speed; joins keep data normalized.
- $lookup joins data at query time.
- Choose based on data size, update frequency, and query needs.
Full Transcript
This visual execution shows how MongoDB decides between embedding related data inside one document or using references with joins via $lookup. The flow starts by identifying the relation type. For small, tightly connected data, embedding is best. For large or many related items, references and $lookup joins are used. The sample aggregation query uses $lookup to join orders with product details. The execution table traces reading an order, performing $lookup to fetch product info, and adding it to the order document. Variables track the order document and product info as they change. Key moments clarify why $lookup is used instead of embedding and when embedding is preferred. The quiz tests understanding of the join steps and embedding impact. The snapshot summarizes the decision rules and trade-offs between embedding and joins in MongoDB.