Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

Model One-to-Many Relationships with Embedded Documents

Use embedded documents for one-to-many relationships. Embedding connected data in a single document reduces the number of read operations required to retrieve data. Structure your schema so your application receives all required information in a single read operation. For example, use the embedded one-to-many model for the following relationships:

  • Country to major cities

  • Author to books

  • Student to classes

The example schema contains three entities, with address one and address two belonging to the same patron:

// patron document
{
_id: "joe",
name: "Joe Bookreader"
}
// address one
{
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
}
// address two
{
street: "1 Some Other Street",
city: "Boston",
state: "MA",
zip: "12345"
}

In this example the application needs to display information for the patron and both address objects on a single page. To retrieve all necessary information with a single query, embed the address one and address two information inside the patron document:

{
_id: "joe",
name: "Joe Bookreader",
addresses: [
{
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
},
{
street: "1 Some Other Street",
city: "Boston",
state: "MA",
zip: "12345"
}
]
}

Back

One-to-One Embedded Documents

On this page