Modeling 1 - 1 Relationship
·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.
The id of the country is referenced inside the capital city doc.
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.