Document References vs Embedding
Data modeling in MongoDB involves deciding between Embedding (nested schemas) and Referencing (linking documents using IDs).
1 Reference Modeling Syntax
JSON — Linked Documents
# User Document
{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"name": "Balaji"
}
# Post Document referencing User
{
"title": "Learning MongoDB",
"author_id": ObjectId("507f1f77bcf86cd799439011")
}
2 Code Challenge
Challenge: Explain when embedding is a better model choice than referencing, and when referencing should be preferred for data consistency.