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




