liveBook · Manning

One-to-many relationship

As explained in MongoDB 8.0 in Action, Third Edition

A one-to-many relationship associates one document or field with multiple related documents or subdocuments.

A one-to-many relationship connects one document or document field to multiple related documents or subdocuments. In an airline route system, one airline operates multiple routes. The relationship guides how related data is organized and accessed in MongoDB. section 5.1.2

When the child data is consistently accessed with its parent, embedding it in an array or subdocument commonly reduces queries and permits a single atomic update. Use references instead when the related entity is often accessed independently, duplication is costly, or the data could make the parent document too large. section 5.1.2 section 5.2

Choose a representation by considering access patterns and document limits:

  • Embed the many side when the child records belong naturally to the parent and are usually retrieved or displayed together. section 5.2
  • Reference the related records when they are frequently queried alone, when embedding would duplicate data without meaningful read benefits, or when relationships are complex. section 5.2
  • Check document growth: MongoDB documents must remain below the 16 MB maximum BSON document size. section 5.2

Airlines and routes

Model an airline with an embedded collection of routes when route details are normally shown in the airline context. Keep airports as separately identifiable documents when departure and arrival airports participate in many routes. A referenced flight document can identify an airport with $ref, $id, and $db fields. section 5.1.2 section 5.2

{
  "_id": ObjectId("56e9b39b732b6122f877facc"),
  "flight_id": "FL123",
  "airline": {
   "id": 410,
   "name": "Delta Airlines",
   "alias": "2B",
   "iata": "ARD"
              },
  "src_airport": "JFK",
  "dst_airport": "LAX",
  "codeshare": "",
  "stops": 0,
  "airplane": "ATP"
}

Inspect a flight document that embeds airline details while retaining airport identifiers, illustrating a practical related-data structure. section 5.1.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains embedding and referencing trade-offs, atomic writes, query counts, duplication, and the 16 MB BSON limit.
Examples
Uses airlines with routes, airports with flights, contact addresses, and referenced MongoDB documents.
What it assumes
Analyze how entities interact and which queries and operations the application performs.
What you can do after
Evaluate/understand it by choosing embedded or referenced MongoDB data based on access patterns, duplication, complexity, and document size.
How it connects
Connects embedding, referencing, denormalized data models, normalized data models, and many-to-many relationships.

Also explained in

Compare how 12 books explain one-to-many relationship →

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.