NoSQL Data Modeling
Shape documents around how they'll be read, often embedding related data instead of splitting it across tables.
What is it?
Relational design (normalization) starts from the data's structure and removes duplication, relying on joins to reassemble it. Document databases (like MongoDB) flip this. There's no cheap, universal join across collections, so you shape each document around how it will be read. It's normal, even encouraged, to embed related data inside one document instead of referencing a separate one.
The key question: is this related data usually read with its parent?
- If yes, embed it (nest it in the same document) to avoid extra lookups. - If it's large, changes independently, or is shared by many parents, reference it (store just an id, like a foreign key).
Explain like I'm 10
A normalized schema is a filing cabinet where each fact lives in one folder, and you cross-reference folders for the full picture. A document database hands someone a ready-made packet with everything stapled together: faster to hand over, but if a stapled-in fact changes, you may have to redo several packets.
Examples
Embedding — a blog post with its comments
// A single document in a "posts" collection
{
"_id": "post_123",
"title": "Why We Chose Postgres",
"body": "...",
"comments": [
{ "author": "Kenji", "text": "Great write-up!" },
{ "author": "Priya", "text": "Curious about your indexing strategy." }
]
}Comments are almost always read with their post and rarely queried alone, so embedding them avoids a second lookup.
Referencing — a post and its author
// posts collection
{ "_id": "post_123", "title": "Why We Chose Postgres", "author_id": "user_45" }
// users collection
{ "_id": "user_45", "name": "Amara Musa", "bio": "Backend engineer..." }An author is shared by many posts and has a full profile that changes on its own schedule. Referencing by id, instead of embedding the user into every post, avoids duplicating and re-syncing the profile everywhere.
How it works
Document databases usually can't join across collections efficiently the way relational databases can. Fetching a document cheaply returns only its own fields. Embedded data comes along for free; referenced data needs a follow-up query (or, in some databases, a more limited join-like operation).
So modeling weighs that against the cost of duplication. Embedding trades some duplicated, harder-to-update data for fewer, faster reads. Referencing trades an extra lookup for a single source of truth, closer to a normalized relational table.
Why does it exist?
Some data is almost always "read all together" (a post and its comments, a cart and its line items). Forcing it into a fully normalized, multi-table design adds join overhead to avoid duplication that may barely matter if the embedded data rarely changes on its own. Document modeling lets the data's shape follow how it's actually read.
When to use it
Embed related data when it's almost always read with its parent, doesn't grow without limit, and is rarely queried or updated on its own: comments on a post, line items on an order, an address on a profile. Reference by id when the data is large, shared by many parents, updated independently, or often queried on its own.
When not to use it
Don't embed data that grows without limit (like comments on a very popular post, which can bloat a document or hit size limits), or data that must be updated across many parent documents at once. Those belong in a separate, referenced collection.
Common mistakes
Embedding a list that can grow forever (like comments on a viral post), until the document hits a size limit or loads slowly.
Carrying relational normalization habits into a document database, ending up with too many references and losing the performance documents offer.
Embedding data shared by many parents (like product details inside every order containing it), then updating it in many places when it changes.
Practice exercises
- Easy:
For a shopping cart with line items, decide whether to embed the line items in the cart document or reference them, and explain why.
- Medium:
Design a document shape for a 'product review' in an e-commerce app, deciding whether the reviewer's profile should be embedded or referenced.
- Hard:
Describe a realistic case where embedding seemed convenient at first but caused problems as the app grew, and how you'd redesign it.
Interview questions
What's the difference between embedding and referencing in document database design?
Embedding nests related data inside the parent document, so one read fetches both. Referencing stores just an id pointing to a separate document, which costs an extra lookup.
Why is NoSQL document modeling often described as designing around access patterns rather than around normalization?
Without cheap joins across collections, the right shape depends on how the data is read together, not on removing duplication as relational normalization does.
What's a risk of embedding an unbounded list inside a document?
The document keeps growing until it's slow to load or hits the database's maximum document size.
What does 'design for your queries first' mean in document modeling, and how does that differ from a relational approach?
Start from how the data will be read and shape the document around that. Relational design starts from the data's structure, normalizes it, and relies on joins to build whatever shape each query needs.
What's the 'extended reference' pattern, and what problem does it solve?
Storing a few duplicated fields (e.g. a customer's name) from a referenced document next to its id. Common reads then skip the extra lookup for fields that are needed constantly and rarely change.
Why is a duplicated field in an extended reference a deliberate tradeoff rather than a mistake?
It accepts the cost of syncing the copy (only when the source changes) to skip an extra lookup on every read. Worth it when reads far outnumber updates to that field.
How would you model a many-to-many relationship, like articles and tags, in a document database?
Store an array of tag ids (or names) on each article. You don't need arrays on both sides, only the direction you query from. A separate tags collection can hold tag details if needed.
What is the 'bucket pattern', and what kind of data does it fit?
Grouping many small time-stamped readings (like sensor data) into one document per time period (e.g. one per hour, holding an array of that hour's readings) instead of one document per reading.
Why does the bucket pattern reduce overhead compared to one document per reading?
Fewer, larger documents mean fewer reads and writes and less per-document storage overhead than many tiny documents.
Why does a document size limit (e.g. MongoDB's 16MB per document) shape modeling decisions?
It caps anything embedded. If an embedded list or nested structure could realistically grow past the limit, reference that data separately instead.
Why can updating a single fact that's duplicated across many parent documents become expensive or error-prone?
Every document holding a copy must be found and updated, instead of changing one row in one place. Miss one and it silently goes stale.
What's a 'polymorphic' document pattern, and when is it useful?
Storing different kinds of items (a video, an article, an image) in one collection, with shared common fields plus type-specific ones. Useful when the app queries them together as one feed far more often than it needs their differences.
Why do document databases typically still support secondary indexes even though they don't support cheap arbitrary joins?
Indexes speed up searching one collection by a field other than its id. That's unrelated to joins; you still need fast lookups within a collection even if you avoid joining across collections.
What's the practical effect of embedding data that's frequently updated independently of its parent, like a product's live inventory count embedded in every historical order line item?
Every change would require updating every document holding a copy. For a fast-changing shared value like stock count, that's a sign to reference it, not embed it.
Why is embedding often described as trading read performance for write/update complexity, and referencing the reverse?
Embedding gets everything in one read, but updates to shared data must reach every copy. Referencing keeps one easy-to-update source of truth, but every read of the related data costs an extra lookup.
What does 'atomic update' mean for a single document, and why does that favor embedding data you need to update consistently together?
Most document databases make an update to one document all-or-nothing. Data embedded in one document gets that guarantee for free. Updating two referenced documents consistently needs extra coordination (like a transaction) that isn't automatic.
Why might a shopping cart's line items be a good candidate for embedding, but a product's own catalog details a bad one to embed inside every cart referencing it?
Line items belong to one cart and are read and written with it, so embedding is safe. A product's price and description are shared by every cart and order that references it and change on their own, so embedded copies would need updating everywhere.
What's the risk of denormalizing a frequently-changing field, like a current stock count, into many documents that reference the same product?
Every copy must be updated whenever the count changes. If even one update is missed or late, that document shows a wrong stock count.
How does a document database's lack of foreign key constraints change how you enforce referential integrity between referenced documents?
Nothing in the database stops you storing an id that points at a missing (or later deleted) document. Keeping references valid becomes the app's job.
Why can schema flexibility be both an advantage and a risk in a document database?
You can change a document's shape without a formal migration, which helps fast iteration. But nothing stops documents in one collection from drifting into inconsistent shapes if the app isn't careful.
What's a practical problem caused by documents in the same collection having drifted into different shapes over time?
Code reading the collection must handle several shapes for the same kind of document (a missing old field, a renamed field, a changed nested structure) instead of assuming one structure.
Compare a document database's modeling approach to a plain key-value store's — what extra structure does a document format give you?
A key-value store's value is an opaque blob the database can't look inside. A document is structured, so the database can query, index, and update individual fields without the app loading and parsing the whole thing.
How does a wide-column store's modeling approach differ from a document database's for the same 'posts and comments' example?
A wide-column store usually has one table per query it must serve, duplicating data into purpose-built tables (e.g. one keyed for 'comments by post', another for 'comments by author'), rather than one flexible document read a few ways.
Why does sharding in a NoSQL database depend heavily on choosing a good partition/shard key, and how does that connect to access-pattern-driven modeling?
Data is spread across nodes by that key, so a query that doesn't filter on it may have to check every shard instead of one. The same 'design around how it's queried' principle behind embed-vs-reference also drives the shard key choice.
What's the risk of choosing a partition/shard key that doesn't match your most common query pattern?
Your most frequent query scatter-gathers across every shard instead of hitting one, losing the scalability a well-chosen key should give.
Scenario: a social app embeds a `likes` array of user ids directly inside each post document. What happens as a post goes viral, and how would you redesign it?
The array grows without limit, so the post gets slower to load and update and may approach the document size limit. Move likes to a separate likes collection (one small document per like, referencing the post id) so the post stops growing.
After redesigning likes into a separate collection, how do you still cheaply show a '142 likes' count without counting that collection on every read?
Keep a denormalized counter on the post, incremented atomically on each like. This hybrid embeds just the summary and references the detailed records.
In a database that offers only eventual consistency across replicas, why can a denormalized duplicate field briefly show stale data after its source is updated?
A change isn't guaranteed to reach every replica at the same instant. A read from a replica that hasn't caught up can briefly see the old value.
What's the single biggest mindset shift a relational-schema-experienced developer has to make when modeling for a document database?
Stop asking 'what's the cleanest, duplication-free way to represent this?' and start with 'how will this be read, and what must sit right next to it to avoid an extra lookup?'