E · Application data design

Model orders around history, growth, and atomic changes

“Embed or reference?” has no universal answer. A checkout record needs a stable view of what was purchased, while a product catalog needs one current shared version. This example makes those access and history requirements explicit.

Original study guide · Reviewed against official documentation on November 17, 2026

Begin with the questions the application asks

An order page needs to show the items, quantities, and prices agreed to at checkout. A product page needs the current catalog price and description. Support staff may also need to find a customer's orders. These are related facts, but they change at different times and are read through different paths.

{
  _id: ObjectId("..."),
  customerId: ObjectId("..."),
  status: "paid",
  placedAt: ISODate("2026-11-17T09:00:00Z"),
  items: [
    {
      productId: ObjectId("..."),
      sku: "book-1",
      titleAtPurchase: "MongoDB Basics",
      quantity: 2,
      unitPriceAtPurchase: Decimal128("18.00")
    }
  ],
  totalAtPurchase: Decimal128("36.00")
}

The order embeds its bounded line items and snapshots values that must remain historically true. It references the customer and product with IDs so the application can still open the current account or catalog record when needed. The snapshot is not a second source of the current product price; it is evidence of the price used in this transaction.

Embedding is useful when the child set is bounded

Keeping a small order's items together often makes the receipt a single-document read. Updates to that order document are atomic at the document level. This also avoids a second query just to display a handful of line items that have no useful life outside the order.

The same argument does not justify embedding every order ever placed in the customer document. That array grows without a known bound, makes the customer document larger over time, and couples a frequently accessed customer record to a high-volume history. Store orders in their own collection with a customerId index for customer history instead.

Choose a source of truth for each field

If the current product title changes, an old receipt should not silently change too. titleAtPurchase and unitPriceAtPurchase are snapshots; productId points to the canonical product for current inventory or current description. Define which value each screen should display. Otherwise developers may unknowingly replace the order snapshot with today's catalog value.

This is intentional duplication, not an invitation to copy every product field into every order. Snapshot only the facts required for the historical record. If a duplicated value is meant to remain synchronized rather than frozen, define how and when it is refreshed.

Tempting answers that fail

Storing only productId and looking up the live price on every receipt loses the historical checkout price when the catalog changes. Embedding a customer's entire order history avoids one query but creates an unbounded array and a hot, ever-growing parent document. Splitting every field into a separate collection can add joins and coordination to reads that the application performs together.

Transactions are useful when an invariant truly spans documents or collections, such as recording a payment and changing separately stored inventory as one logical operation. They are not a substitute for choosing a model that makes the common order write a single-document change. Design the boundary from the invariant, then use a transaction when the invariant cannot fit in one document.

Check your understanding

Question: Which price should an old order receipt show after the product catalog price changes?

Answer: The receipt should show the order's unitPriceAtPurchase. The catalog's current price belongs to the product record and serves current product views, not a historical checkout record.

Further reading