Embedding: One-to-Few Relationships
Learners will embed sub-documents for tightly coupled data and measure the read-performance advantage of co-locating related information.
What Is Document Embedding?
Embedding means storing related data directly inside a parent document rather than in a separate collection. Instead of linking two collections with a foreign key, you nest the related data as a sub-document or an array of sub-documents. This is MongoDB's most powerful design tool because it allows a single read to retrieve the parent and all its related data at once.
The One-to-Few Relationship
A one-to-few relationship exists when one parent document has a small, bounded number of child items—typically fewer than a few dozen. Classic examples include a blog post and its comments, a user and their addresses, or an order and its line items. When the child count is predictable and small, embedding is almost always the right choice.
All lessons in this course
- Embedding: One-to-Few Relationships
- Referencing: One-to-Many and Many-to-Many
- The Unbounded Array Anti-Pattern
- Schema Design Decision Framework