Karthik Sai
Learners Blog

Learners Blog

Modeling 1 - 1 Relationship

Modeling 1 - 1 Relationship

Karthik Sai's photo
Karthik Sai
ยทFeb 18, 2022ยท

1 min read

If there is one to one relationship there are two ways to represent the data.

  • Embed the document
  • Add an id-reference to the document.

A few examples of this 1-1 relationship.

  • Country - capital city: Every country has only capital and each capital is associated with one country only.
  • Person - their iris. A person has a unique digital representation of his iris. Each iris corresponds to only a single person.

Embedding the capital city doc inside the country doc. carbon (1).png

The id of the country is referenced inside the capital city doc. carbon (3).png

In 1-1 relationship scenarios it is better go with embed document approach. It requires only one database query. If the id reference approach is taken it will take two database queries to fetch the whole data which is not a good approach from performance point of view.

ย 
Share this