Skip to content

Lesson 1 of 9 · 25m

Documents Are Access Patterns, Not Entities

Start with reads, writes, growth, and consistency boundaries instead of translating relational tables into JSON-shaped rows.

The fastest way to build a bad MongoDB application is to treat a document as a row with curly braces. I have watched teams recreate users, user_addresses, user_preferences, and user_sessions as four collections, then celebrate because every lookup was technically a document query. They had built a relational database with worse tooling. MongoDB rewards a different starting question: what does the application need together, at the same time, under the same consistency boundary?

Start with the workload, not the nouns

Before creating collections, write down the top access paths:

read order with customer summary and line items      very frequent
append shipment event to order                        frequent
list latest 20 orders for customer                    frequent
recalculate inventory across warehouse                rare but exact
search orders by arbitrary fields                     rare, administrative

That inventory tells you what belongs in one document, what can be referenced, and what does not belong in MongoDB at all. If a page load needs five round trips to assemble one logical object, your model is probably too fragmented. If every small write updates a giant document, your model is probably too coarse.

A document is the atomic unit

MongoDB guarantees atomicity for operations on a single document. That is the core modeling primitive:

db.orders.updateOne(
  { _id: ObjectId("665f1f9bbfd34cf7b2785d21"), "items.sku": "SKU-1042" },
  { $inc: { "items.$.quantity": 1 }, $set: { updatedAt: new Date() } }
)

No transaction is required because one document changed atomically. Put state that must change together inside the same document when practical. Conversely, do not use document atomicity as an excuse to stuff unrelated data together; giant hot documents become contention magnets.

Model the real ownership boundary

An order may embed line items because they are meaningless outside the order. A customer record should usually be referenced because customers live longer than orders and change independently. Ownership is not the same as foreign-key membership. Ask who creates, updates, archives, and deletes each piece of data.

{
  _id: ObjectId(),
  customerId: ObjectId("665f1c22bfd34cf7b2785c80"),
  status: "packed",
  items: [
    { sku: "SKU-1042", name: "USB-C cable", quantity: 2, unitPrice: 900 }
  ],
  totals: { subtotal: 1800, currency: "USD" },
  createdAt: ISODate("2026-11-30T18:04:12Z")
}

This shape supports the common reads. It also duplicates some customer data if needed for display. Duplication is a design decision, not a moral failure.

Beware unbounded arrays

Arrays that grow forever are a classic MongoDB failure. Activity feeds, events, messages, and metrics can make documents hit the 16 MB document limit or become expensive to move and update. Bucket them, archive them, or store them as separate documents keyed for range reads.

Field notes

  • Normalize for invariants, denormalize for reads, and write down which copy is authoritative.
  • Document atomicity is powerful but not a substitute for business-level consistency.
  • A document shape optimized for one screen can make background jobs painfully expensive.
  • If you cannot name the top five access patterns, you are not modeling yet — you are decorating JSON.

Was this lesson helpful?

Sign in to rate