MongoDB & NoSQL (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Data Modeling: Embedding vs Referencing & The 16MB Document Limit.
senior LevelmongodbAdvanced Internals
In Simple Words:
- Embedding (Denormalized): Stores sub-documents inside the parent document. Provides single-operation reads (no joins). Best for 1:1 and bounded 1:N relationships (e.g. user address, line items in an order).
- Referencing (Normalized): Stores
ObjectIdforeign keys and joins using$lookup. Mandatory for unbounded 1:N relationships (e.g. millions of sensor logs) to prevent breaching MongoDB's hard 16MB BSON document size limit.