Schema Design Patterns
10 min read
Common Schema Design Patterns
MongoDB schema design is application-driven. Several recurring patterns solve common problems elegantly.
- Bucket pattern — group time-series data into buckets (e.g. one document per hour of sensor readings) to avoid document-per-reading explosion
- Subset pattern — embed only the most recently accessed subset of a large array (e.g. last 10 comments) and store the rest in a separate collection
- Computed pattern — cache expensive aggregation results in the document and update them on write
- Outlier pattern — handle the rare document that breaks your size assumptions with an overflow flag and a linked collection
- Extended reference pattern — embed a small copy of the most-read fields from a referenced document to avoid frequent
$lookupcalls
Students in CPCODELAB's database module go deeper into these patterns with real dataset exercises. The key insight is: model data for how you query it, not for how it exists in the real world.
Ready to test yourself?
Practice MongoDB— quiz & coding exercises