MongoDB Schema Design Patterns for Real Products

MongoDB Schema Design Patterns for Real Products

Embed or reference? The patterns that keep MongoDB fast as a product grows from MVP to real traffic.

MongoDB is flexible, which is exactly why schema design matters. The question is never "what can I store?" but "how will I read it?"

Design for your queries

List the screens and API calls first. Data that is always read together should usually live together. Data that grows without limit should live apart.

Embed when it is bounded and owned

An order's line items belong to the order, are read with it and never grow past a reasonable size. Embedding them means one read per order page.

Reference when it is shared or unbounded

Products are shared across many orders, and a user's messages grow forever. Those get their own collections with indexed references.

Patterns worth knowing

  • Extended reference: copy a few frequently read fields, like a product name and price at purchase time, into the referencing document.
  • Subset: embed the latest few items, such as recent reviews, and keep the full list in its own collection.
  • Bucket: group time-series events, such as tracking updates, into documents per hour or day.
  • Computed: store totals and counters that would be expensive to calculate on every read.

Watch the 16 MB limit

If a document can grow without a bound, it will eventually hit the 16 MB document limit or become slow to update. That is the clearest signal to split it out.

More on this topic

Photography
3D Models
Development
Illustrations
Fashion
Digital Art
Packaging
Motion
Illustrations
Video Production
Photography
3D Models
Development
Illustrations
Fashion