Embedding vs Referencing
11 min read
Data Modeling: Embedding vs Referencing
One of the most important MongoDB design decisions is whether to embed related data inside a document or store it separately and reference it by _id. The right choice depends on access patterns, document size, and how often the data changes.
Embedding (Denormalisation)
Embed when the sub-data is always accessed with the parent, changes together with it, and the total document stays under 16 MB.
js
// Embed address directly in the student document
{
name: "Aman Verma",
address: {
street: "45 Lake View",
city: "Pune",
pincode: "411001"
}
}Referencing (Normalisation)
Reference when the related data is large, shared by many documents, or updated independently.
js
// Store courseId as a reference
{
name: "Priya Singh",
courseId: "fs101" // ObjectId or string key
}
// Courses live in their own collection
{ _id: "fs101", title: "Full Stack", instructor: "Rohan" }Rule of thumb: embed for one-to-few, reference for one-to-many or many-to-many. Optimise for the most common read pattern.
Ready to test yourself?
Practice MongoDB— quiz & coding exercises