liveBook · Manning

Many-to-many relationship

As explained in MongoDB 8.0 in Action, Third Edition

A many-to-many relationship allows multiple documents on each side to be associated with multiple documents on the other side.

A many-to-many relationship associates multiple documents on each side with multiple documents on the other. In MongoDB, it commonly uses references between collections and may require more involved querying and indexing. section 5.1.2

Choose the representation according to access patterns and data complexity:

  • Prefer embedding when related data is retrieved together in a single read and does not create oversized or bloated documents. section 5.1.2 section 5.2
  • Use normalized documents and references when entities are accessed independently, duplication is costly, or the many-to-many relationship must be represented accurately. section 5.2
  • Store another document's _id as a manual reference, or use a DBRef when references span collections or databases; resolving either requires additional queries. section 5.2
  • Use $lookup for same-database joins and $graphLookup for recursive exploration of hierarchical or graphlike relationships. section 5.2

Airports and routes

Airports-to-routes is many-to-many because an airport can serve multiple routes and each route connects airports as departure and arrival points. A route document can hold airport identifiers, while airport documents remain separately addressable. section 5.1.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"
}

Use the route-style document to inspect how flight data identifies its source and destination airports. section 5.1.2

{
   "_id": "JFK",
   "name": "JFK International Airport",
   "location": {
      "city": "New York",
      "country": "USA"
   },
   "facilities": ["Wi-Fi", "Lounge", "VIP Services"]
}

Use the airport document to compare the separately stored entity with the airport identifiers referenced by routes. section 5.1.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains embedding, references, DBRefs, and join stages, while stopping short of a complete reference-array schema or index design.
Examples
Uses airline routes and airports, including flight and airport documents, to identify and represent the relationship.
What it assumes
Assumes familiarity with MongoDB documents and collections while defining relationship types and explaining embedding and references.
What you can do after
Evaluate whether to embed or reference related MongoDB documents and select an appropriate join or reference mechanism.
How it connects
Connects many-to-many relationships with schema design, embedding, normalized data models, manual references, DBRefs, and aggregation joins.

Also explained in

Compare how 5 books explain many-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.