MongoDB Data Modeling Patterns
On this page
MongoDB schema design patterns
Day 4 covered the basic choice between embedding and referencing. Real applications hit the same few problems again and again, and MongoDB's community has named the solutions. Each pattern trades something (duplication, write cost, complexity) for faster or simpler reads.
The bucket pattern for time series data
A sensor sends a reading every second: 86,400 tiny documents per device per day, each with its own _id and index entries. Group them instead, one document per device per hour:
{ deviceId: "pump-17", hour: ISODate("2026-10-10T09:00:00Z"), count: 3600,
readings: [ { t: 0, temp: 41.2 }, { t: 1, temp: 41.3 } /* ... */ ],
min: 40.9, max: 43.0, sum: 148230.5 }Fewer documents and index entries, and per-hour summaries ready to read. The bucket is bounded (at most 3,600 readings), avoiding the unbounded-array trap. For new time-series workloads, use MongoDB's time series collections, which apply this bucketing automatically and compress well.
The attribute pattern for varied fields
A catalog where drills have voltage and torque, shirts have size and color, and lamps have wattage. Indexing every possible field is impossible. Store them as key-value pairs:
{ sku: "DRL-200", name: "Cordless drill",
attrs: [ { k: "voltage", v: 18 }, { k: "torque_nm", v: 60 }, { k: "color", v: "green" } ] }
db.products.createIndex({ "attrs.k": 1, "attrs.v": 1 })
db.products.find({ attrs: { $elemMatch: { k: "voltage", v: 18 } } }) // $elemMatch: Day 6One compound index serves queries on any attribute. (A wildcard index on a nested object is an alternative.)
The computed pattern
Counting a post's comments or summing a product's ratings on every page view is wasteful. Store the result and update it on write:
db.products.updateOne({ _id: 7 }, { $inc: { "ratings.count": 1, "ratings.sum": 5 } })
// average = sum / count, computed cheaply on readFor values that can be slightly stale, recompute periodically with $merge (Day 16) instead.
Extended reference, outlier and schema versioning patterns
Extended reference. Copy the few fields of a referenced document you always display (an order stores customerName alongside customerId), avoiding a $lookup on every read (Day 4).
Outlier. Most books have a few hundred buyers; one bestseller has two million. Design for the typical case (embed buyer ids in the book), and for outliers set a flag (hasOverflow: true) and move the excess into separate overflow documents. Don't design every document around the rare extreme.
Schema versioning. Add schemaVersion: 2 to documents written in a new shape. Readers handle both versions, and documents are migrated lazily when touched or in a background job. Shape changes happen without downtime or a big-bang migration.
Which MongoDB pattern to use
| Problem | Pattern |
|---|---|
| Millions of small, time-ordered records | Bucket (or a time series collection) |
| Documents with very different fields to search | Attribute |
| Expensive values read far more than written | Computed |
| Always showing a few fields of a related document | Extended reference |
| A few documents far larger than the rest | Outlier |
| Changing document shapes in production | Schema versioning |
Check yourself
It cuts the number of documents and index entries by thousands, keeps each array bounded at 3,600 readings, and lets you store per-hour summaries like min, max and average alongside them. MongoDB's time series collections apply the same pattern automatically.
Attributes become { k, v } pairs in an array, and one compound index on attrs.k and attrs.v serves queries on any key.
Changing document shapes gradually: readers handle each version they know, and documents are migrated lazily, when touched, or by a background job, instead of one risky migration over the whole collection.
Storing a value that's expensive to calculate, such as a product's review count and average rating, and updating it on write, instead of recomputing it on every read.
Designing for the common case, such as a book with a few hundred buyers, and handling rare huge cases separately, for example by flagging the document and moving the overflow into extra documents.