Skip to content

Lesson 2 of 9 · 26m

Embedding vs. Referencing Without Doctrine

Use cardinality, ownership, update frequency, and growth to choose between embedded subdocuments and references — not ideological purity.

New MongoDB teams ask whether embedding or referencing is "best" as if there is a universal answer. There is not. There are only trade-offs across cardinality, ownership, consistency, and update patterns. The wrong answer usually comes from optimizing for elegance instead of operational behavior.

Embed what is owned and bounded

Embedding is strongest when child data is conceptually part of the parent, usually read with the parent, and bounded in size:

{
  _id: ObjectId(),
  sku: "SKU-1042",
  title: "USB-C cable",
  dimensions: { weightGrams: 48, lengthMm: 1200 },
  variants: [
    { color: "black", stockKeepingUnit: "SKU-1042-BLK" },
    { color: "white", stockKeepingUnit: "SKU-1042-WHT" }
  ]
}

Variants are acceptable here if there are a handful. They are unacceptable if a product can accumulate thousands of supplier-specific variants updated independently.

Reference what is independent or reused

Use references when child data has its own lifecycle, is shared across parents, or grows without bound:

// customers
{ _id: ObjectId("665f1c22bfd34cf7b2785c80"), email: "ops@example.net" }
// orders
{ _id: ObjectId(), customerId: ObjectId("665f1c22bfd34cf7b2785c80"), status: "paid" }

This costs extra reads or an aggregation $lookup, but it avoids rewriting every order when a customer changes their display name. Do not fear a second query when it protects a clean ownership boundary.

Duplication is sometimes the correct write amplification

Read-optimized duplication is common in mature systems. If an invoice must forever show the billing address as it existed when issued, copy that address into the invoice. If a feed needs actor names on millions of rows, consider storing a snapshot and accepting stale display values or repairing them asynchronously.

copy when historical truth matters
reference when current shared truth matters
embed when the child cannot exist meaningfully alone
separate collection when growth is unbounded or access is independent

Beware the $lookup crutch

$lookup is useful for reporting and occasional joins, but a hot application path built around multi-collection joins is often a modeling error. It adds pipeline complexity and can defeat index intuition. If every request needs the same join, either the read model is wrong or MongoDB is being asked to behave like a relational database without relational planner maturity.

Practical cardinalities

Use rough numbers, not feelings:

1:1 or 1:few, bounded       usually embed
1:many, bounded tightly     embed if read together
1:many, unbounded           separate collection
many:many                   separate collections or relational design
shared reference data       reference or cache, not deep embedding

The line is not exact, but pretending cardinality does not matter is how applications become migrations waiting to happen.

Field notes

  • Embedding does not mean "never duplicate"; it means "choose which facts travel together."
  • Large arrays slow updates, increase document churn, and complicate concurrency.
  • $lookup is not forbidden; it is a warning sign when it sits in every latency-sensitive request.
  • Revisit the model after real traffic arrives. Whiteboard cardinalities are always optimistic.

Was this lesson helpful?

Sign in to rate