MongoDB Data Modeler Associate cheat sheet
MongoDB Data Modeler Associate · Keep the key ideas within reach.
Data Modeling
Cardinality rule: 1:1 → embed; 1:few → embed array; 1:many (unbounded) → reference from the many side; many:many → reference both sides.
Access pattern first: enumerate every query and update with its frequency and expected result size before choosing embed vs reference.
BSON document hard limit is 16 MB; arrays that grow without bound risk hitting it, so cap or bucket them.
Unbounded array anti-pattern: a document whose array grows per event (log lines, comments) should be split into a parent plus a child collection.
Duplication is acceptable when the duplicated field is read far more often than it changes; note the write cost of updating every copy.
Schema versioning: add a schema_version integer field so mixed-shape documents can be migrated lazily on read.
Model for the workload, not for normalization: joins via $lookup exist but cost a second collection scan per input document.
Computed pattern: precompute and store frequently read aggregates (counts, totals) and update them in the same write path.
Schema Design Patterns
Bucket pattern: group time-series or IoT events into N events per document (e.g. 200/hour) to cut document count and index size.
Outlier pattern: handle the rare document that exceeds normal bounds with an overflow flag plus a companion collection.
Subset pattern: store the 10-20 most recent reviews inline and the full set in a separate collection to keep working set small.
Extended reference: embed only the fields of the referenced document that queries need (e.g. name, price) and keep the full doc separately.
Computed pattern: store derived values (average rating, comment count) to avoid recomputing on every read; refresh on write or on a schedule.
Attribute pattern: move a small, known set of varying fields into a key/value array (k, v) so one index covers all of them.
Polymorphic pattern: one collection holds documents of differing shape discriminated by a type field; index on that field for per-type queries.
Schema versioning pattern: pair a version field with a migration-on-read so deployment and data migration are decoupled.
Document Structure and Relationships
Embedded documents are read atomically with the parent; referencing requires a second query or $lookup.
One-to-many with a bounded 'few' (e.g. an address list) embeds; one-to-many with thousands of children references from the child.
Many-to-many: store an array of _id values on one side and query with $in, or use a separate join collection when edge data is needed.
Array of embedded docs: query with dot notation ("items.sku") and positional operator ($) or $elemMatch for element-level updates.
Parent references pattern: store both the child's parent _id and an ancestor path array for fast subtree queries.
MongoDB is schema-flexible but not schema-free in practice: enforce shape in application code or with $jsonSchema validators.
Subdocument update: use $push with $each and $slice to keep arrays bounded at write time.
No transactions across collections needed for single-document atomicity; multi-document transactions exist but carry latency cost.
Indexing for the Data Model
ESR rule for compound indexes: Equality fields first, then Sort fields, then Range fields.
Prefix rule: a compound index {a:1,b:1} serves queries on {a} but not on {b} alone.
Multikey index: indexing an array field creates one index entry per array element; compound multikey indexes allow at most one array field.
Covered query: all fields in the query and projection are in the index, so the index alone answers it (no document fetch).
Unique index enforces uniqueness on the indexed field(s); sparse/partial indexes index only documents matching a filter.
Text index: at most one per collection; supports $text with $search and weights for field relevance.
TTL index: single-field date index with expireAfterSeconds; the TTL monitor deletes documents roughly every 60 seconds.
Index cardinality matters: low-cardinality fields alone make poor indexes; place them after high-cardinality equality fields.