Model One-to-One Relationships with Embedded Documents

Overview

This page describes a data model that uses embedded documents to describe a one-to-onerelationship between connected data.

Pattern

Consider the following example that maps patron and addressrelationships. The example illustrates the advantage of embedding overreferencing if you need to view one data entity in context of theother. In this one-to-one relationship between patron andaddress data, the address belongs to the patron.

In the normalized data model, the address document contains areference to the patron document.

  1. {
  2. _id: "joe",
  3. name: "Joe Bookreader"
  4. }
  5.  
  6. {
  7. patron_id: "joe",
  8. street: "123 Fake Street",
  9. city: "Faketon",
  10. state: "MA",
  11. zip: "12345"
  12. }

If the address data is frequently retrieved with the nameinformation, then with referencing, your application needs to issuemultiple queries to resolve the reference. The better data model wouldbe to embed the address data in the patron data, as in thefollowing document:

  1. {
  2. _id: "joe",
  3. name: "Joe Bookreader",
  4. address: {
  5. street: "123 Fake Street",
  6. city: "Faketon",
  7. state: "MA",
  8. zip: "12345"
  9. }
  10. }

With the embedded data model, your application can retrieve thecomplete patron information with one query.