Advanced data modeling for MongoDB.
Instruction: Discuss advanced data modeling concepts and practices for MongoDB, focusing on complex, high-performance use cases.
Updated
Official answer
Model complete workflows and invariants, then choose patterns for measured bottlenecks. Record document cardinality/growth, ownership, update rate, access paths and consistency requirements before selecting embedding or references.
A subset pattern can keep a small recent-item preview beside a parent while storing the full unbounded history separately. A computed pattern can keep a read-optimized summary with explicit refresh/reconciliation rules. A bucket pattern can group bounded related measurements, but supported time-series collections may already provide specialized bucketing.
For an order, embed accepted line-item prices as historical snapshots while referencing current catalog entities. Duplicating a historical fact intentionally differs from duplicating a mutable fact that must stay synchronized. Prevent a retry from creating duplicate orders or double-incrementing summaries.
Choose indexes and shard keys from real predicates, sorting and skew. Partial indexes require compatible predicates; TTL expresses retention and is not a general query-performance feature. Avoid unbounded arrays and hot parent documents.
Test read/write correctness, concurrent updates, missing references and future growth. Measure latency distributions, examined work, throughput and storage/write amplification. A vague “index hit rate” does not prove that a query's chosen plan is efficient.