Lesson 1 of 9 · 20m
Documents Are Access Patterns
The real unit of design in MongoDB is not the noun in your domain model; it is the screen, job, or query that reads and writes it.
The worst MongoDB schemas are usually produced by people trying to design the perfect relational schema without joins. They make one collection per noun, scatter a user across six collections, then discover that rendering a profile page requires four round trips and a parade of $lookup stages. MongoDB is not relational-minus-joins. It is a database where the document boundary should follow the access pattern.
Start with the questions, not the entities
Before creating a collection, write down the operations that matter:
GET /users/:id/profile -> read user + preferences + recent sessions
POST /orders -> write order, items, payment snapshot
GET /admin/orders?status=paid -> paginate orders by status and createdAt
worker: nightly settlement -> scan unpaid orders older than 7 daysThose lines tell you what belongs together. If profile data is read together, changed together, and sized sanely, it probably belongs in one document. If order items are displayed with the order and rarely queried independently, embed them. If inventory must be updated independently by many writers, keep it separate.
A document is a locality decision: data you fetch together and mutate together should live together.
Documents are rich values, not rows
{
_id: ObjectId("665f0a9381c4b7c8e89a2410"),
email: "maya.chen@northbeam.io",
displayName: "Maya Chen",
roles: ["support", "billing-admin"],
preferences: {
theme: "dark",
digest: "weekly"
},
lastLoginAt: ISODate("2026-11-29T18:41:03Z")
}That shape is not sloppy. It captures the object the application actually uses. A relational database would normalize it into user, role, and preference tables because rows are flat. MongoDB lets arrays and subdocuments represent real composition.
The hard limit that shapes design
Documents are limited to 16 MB. That sounds generous until somebody embeds every event, every message, or every audit entry in an ever-growing array. Unbounded arrays also make updates and indexes expensive. If a child collection can grow without bound, it gets its own collection.
A useful rough split:
- Embed when the child data is bounded, read with the parent, and owned by the parent.
- Reference when the child is unbounded, independently queried, or independently contended.
- Duplicate carefully when a read model needs a snapshot, such as copying the customer name onto an invoice. A snapshot is allowed to go stale by design; shared mutable truth is not.
Field notes
- Design from reads and writes, then let entities fall out. Never the reverse.
- "Schemaless" is marketing. Production MongoDB has a schema; it just lives in your application and validation rules instead of the catalog.
- Avoid embedding data updated by a different lifecycle than the parent. That is contention in a trench coat.
- A
$lookupon the hot path usually means the document boundary was drawn in the wrong place.
Was this lesson helpful?