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











