liveBook · Manning

Embedding

As explained in MongoDB 8.0 in Action, Third Edition

Embedding is the schema-design technique of storing related data inside a parent document, typically as a nested document or array.

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

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains selection criteria, performance benefits, duplication risks, relationship limits, and the 16 MB BSON constraint.
Examples
Uses a flight document with an embedded airline subdocument and contrasts it with referenced airport and collection data.
What it assumes
Assumes document and relationship modeling while defining one-to-one, one-to-many, and many-to-many structures.
What you can do after
Evaluate whether related data belongs inside a document or in referenced collections.
How it connects
Connects denormalized data models with subdocuments, one-to-many relationships, normalized models, many-to-many relationships, and $lookup.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.