Catalog My learning Blog
MongoDB Tutorial: Schema Design, Indexing and Sharding · Phase 1 · Lesson 4 · 4 min read

MongoDB Schema Design: Embedding vs Referencing

On this page
  1. MongoDB schema design starts from access patterns
  2. When to embed related data
  3. When to reference another collection
  4. The unbounded array anti-pattern
  5. Subset, extended reference and bucket patterns
  6. Worked example: schema design for a blog
  7. Check yourself

MongoDB schema design starts from access patterns

Relational design starts from the data: normalize it, then write queries. MongoDB design starts from the access patterns: which reads and writes happen most, and what data they need together. The same entities can be modeled very differently depending on that.

For each relationship, ask:

  1. Is the related data read together with the parent? Usually yes → embed.
  2. Is it updated or queried on its own? Often yes → reference.
  3. How many can there be? A few, or bounded → embed is safe. Unbounded → reference.
  4. Is it shared by many parents? Yes → reference (or embed a snapshot).
Embedding keeps related data inside the parent document; referencing stores it in another collection and links by id.
Embedding keeps related data inside the parent document; referencing stores it in another collection and links by id.
Code
// order with its lines and shipping address: always read and written together
{ _id: 991, customerId: 42,
  shipTo: { street: "Main St 1", city: "Berlin" },
  lines: [ { sku: "LAMP-1", name: "Desk lamp", qty: 1, price: NumberDecimal("39.00") } ] }

One read gets everything, one atomic write saves it. Note the line copies the product name and price at purchase time: a deliberate snapshot, correct even if the product changes later.

When to reference another collection

Code
// customers: one document each
{ _id: 42, name: "Sam Lee", email: "sam@example.com" }
// orders reference the customer; a customer can have thousands of orders
{ _id: 991, customerId: 42, ... }

Customers and orders are queried separately, and a customer's orders grow without limit. Embedding all orders in the customer document would be the classic mistake.

The unbounded array anti-pattern

Embedding every comment in a post, every event in a device document, or every order in a customer looks convenient at first. Then:

  • documents grow toward the 16 MB limit (Day 3),
  • every update rewrites a larger document,
  • indexes on array fields grow with every element,
  • reading the parent drags along data you didn't need.

Rule of thumb: if an array can grow without a known upper bound, it belongs in its own collection.

Subset, extended reference and bucket patterns

Subset pattern. Embed the few items you show most often, reference the rest. A product page shows the 5 latest reviews; embed those in the product and keep all reviews in a reviews collection.

Extended reference. Embed a few frequently needed fields of the referenced document alongside its id: an order stores { customerId: 42, customerName: "Sam Lee" } so order lists don't need a lookup. Accept that the copy may need updating if the name changes, or treat it as a snapshot.

Bucket pattern. For time series, group readings into one document per device per hour instead of one per reading, bounded by design (MongoDB's time series collections do this automatically).

Day 17 covers more modeling patterns.

Worked example: schema design for a blog

DataDecisionWhy
Post body, title, tagsIn the postAlways read together
Author name and avatarExtended reference in the postShown with every post; author has their own document
Latest 3 commentsSubset embedded in the postShown on the post page
All commentsSeparate comments collection with postIdUnbounded, paginated, moderated separately
Like countCounter field in the post, $inc on likeRead often, cheap to update

Check yourself

No. Orders are unbounded, since a loyal customer keeps adding them, and they're queried independently, by status or date. Store them in their own collection with a customerId field referencing the customer, and index that field.

The order must show what was bought and paid at that time, regardless of later product changes, and it avoids a lookup on every read.

Embedding only the few most frequently needed related items in the parent, such as a product's ten latest reviews, while keeping the full set in a separate collection. The common page loads with one read, and the parent document stays small and bounded.

The copy can become stale if the original changes, and updating every copy is extra work. It's the right choice when the copy should stay frozen, like a price at purchase, or when the field rarely changes.

When the related data is owned by the parent, read together with it, and bounded in size, such as an order's lines or a user's addresses.