Embedding stores related data inside a parent MongoDB document, usually as a nested document or array. It is a denormalized schema technique for data commonly retrieved together. section 5.1.2 section 5.2
Choose embedding when:
- Entities have a contains relationship, such as a contact containing an address. section 5.2
- A parent has many children that are consistently accessed or displayed in the parent’s context. section 5.2
- A single read and a single atomic write can simplify routine access and updates. section 5.2
Embedding is not universal. Large or weakly related embedded data can bloat documents, duplicate independently used information, or exceed MongoDB’s 16 MB maximum BSON document size. Use references when the related entity is frequently accessed alone or when complex many-to-many relationships require a separate model. section 5.2 section 5.1.2
Embedded airline details
A flight document can contain an airline subdocument with its identifier, name, alias, and IATA code alongside the flight’s airports and aircraft data. The flight and its airline details can then be retrieved together. section 5.1.2
