MongoDB Foundations·Documents with discipline

Schema design patterns

Embed what is read together and reference what grows or is shared.

Beginner 11 min read Lesson 2 of 300 Challenge included
What you will be able to do
  • Explain when embedding beats referencing and vice versa.
  • Match one-to-few data with embedding and one-to-many data with referencing.
  • Accept small duplication when it removes extra reads.
  • Use schema validation to keep document shapes consistent.

In plain English

Embedding means copying related data inside the parent document, so one read gets everything. Referencing means storing only an id that points at a document in another collection. The one-to-few pattern embeds a small, bounded list of related items, while the one-to-many pattern references items that keep growing on their own. Duplicating a little data is often cheaper than a second query, but every copy must be kept in sync. Schema validation lets you set rules on a collection so bad writes are rejected before they land.

Step by step

  1. 1Ask whether the related data is read and written together with its parent.
  2. 2Embed a small, bounded list such as a shopping cart with a few items.
  3. 3Reference a large or shared collection such as customers.
  4. 4Add a $jsonSchema validator to a collection that must keep a strict shape.
  5. 5Revisit the design when a list starts growing fast or is shared by many parents.
design.jsJavaScript
db.orders.insertOne({
  _id: 1,
  customer: "Ada",
  items: [
    { sku: "bike", qty: 1, price: 200 },
    { sku: "helmet", qty: 2, price: 30 }
  ]
})

db.customers.insertOne({ _id: "ada", name: "Ada Lovelace" })

db.orders.insertOne({ _id: 2, customerId: "ada", total: 260 })

db.createCollection("orders", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["customerId", "total"],
      properties: {
        customerId: { bsonType: "string" },
        total: { bsonType: "number", minimum: 0 }
      }
    }
  }
})

Common mistakes

  • Embedding a list that grows forever creates documents that hit the size limit and become slow to update.
  • Referencing everything turns every read into a second query when a simple embed would do.
  • Duplicating a value in many places is fine until it changes, and then every copy must be updated together.
  • Treating schema validation as optional lets malformed documents drift in and forces messy query code later.
Your turn

Model one order

Medium

Insert an order document that embeds two line items, then reference a customer by id. Keep the embedded list bounded and small.

JAVASCRIPT · starter
db.orders.insertOne({})
Check your understanding before moving on.
Knowledge checkQuestion 1

When is referencing the better design over embedding?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 2 of 300 · 0% of course