Model One-to-One Relationships with Embedded Documents
On this page
Create a data model that uses embedded documents to describe a one-to-one relationship between connected data. Embedding connected data in a single document can reduce the number of read operations required to obtain data. In general, structure your schema so your application receives all of its required information in a single read operation. For example, you can use the the embedded one-to-one model to describe the following relationships:
Country to capital city
User account to email address
Building to address
Example
The example schema contains two entities, a patron
and an address
:
// patron document { _id: "joe", name: "Joe Bookreader" } // address document { street: "123 Fake Street", city: "Faketon", state: "MA", zip: "12345" }
Embedded Document Pattern
The address
data is frequently retrieved with the patron
information. To allow
your application to retreive all necessary information with a single query, embed the
address
information inside of the patron
document:
{ _id: "joe", name: "Joe Bookreader", address: { street: "123 Fake Street", city: "Faketon", state: "MA", zip: "12345" } }