Stop 06 · MongoDB Data Modeling & AggregationTopics · 2/8

Embedding vs Referencing

Part of the MERN Stack Roadmap

In short

In MongoDB, the biggest design decision is whether related data lives inside one document or in its own collection. Get it right and pages load with one query. Get it wrong and documents bloat or data goes out of sync.

Embedding stores related data inside one document, which is fast to read. Referencing stores an _id that points to another document, which suits data that is large, shared or grows without limit.

The jaadu packs a pencil box into a small school bag while glancing at a tall row of numbered lockers holding bulky sports gear.

Think of it like this

School bag or school locker

Things you need every day and that fit easily, like your pencil box, go in your school bag (embed). Big things, or things the whole class shares, like the sports kit, stay in the locker and you just carry the locker number (reference).

Watch it happen

Two ways to store an order's address and customer

Embed

  • Shipping address stored inside the order
  • One read gets everything
  • Best for small data read together
  • Snapshot: keeps the address as it was at checkout

Reference

  • Order stores customerId pointing to users
  • Second query or $lookup to fetch the user
  • Best for shared, large or growing data
  • One source of truth, update once

Embed what you read together and what belongs to the parent. Reference what is shared, big, or grows without limit.

The big ideas

What you need to know

Model for how you read

Ask which screens need this data together. Data shown together is a strong case for embedding.

16 MB limit

A document can't exceed 16 MB. Arrays that grow forever, like every comment on a viral post, should be referenced.

Duplication is sometimes right

Copying a product's name and price into an order is correct, because the order must remember the price paid.

Shared data changes

If many documents embed the same user name, a name change means updating all of them. Reference it instead.

See it in code

Walk through the code

js
1// Embedded: address and line items belong to this order2{3  _id: ObjectId("..."),4  customerId: ObjectId("665f..."),            // referenced: users are shared5  shippingAddress: { line1: "12 MG Road", city: "Bengaluru", pin: "560001" },6  items: [7    { productId: ObjectId("..."), name: "Steel Bottle", price: 450, qty: 2 },8    { productId: ObjectId("..."), name: "Notebook", price: 60, qty: 5 }9  ],10  total: 1200,11  createdAt: ISODate("2026-11-02T10:30:00Z")12}

1/4The customer is shared across many orders, so store only their id.

A real order mixes both: embedded address and item snapshots, plus a reference to the customer.

Save yourself the pain

Common mistakes

Oops

Embedding every comment inside a blog post forever.

Do this instead

Comments can grow without limit. Store them in their own collection with a postId reference.

Oops

Splitting everything into separate collections like SQL tables.

Do this instead

MongoDB shines when you embed data you read together. Only reference when there is a reason.

Your mission

Design a food delivery app

~25 min

Quick check

An order needs to show the price the customer paid, even if the product price changes later. What should you do?

Go deeper

  1. 1Embed a delivery address inside an order, but reference the user by _id.
  2. 2Reference comments in their own collection when a post can get thousands of them.
  3. 3Write down the read and update pattern before choosing either option.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge