An Introduction of MongoDB Association

When we create a large application with database then we need to understand how the collection are associated with each other and will be crucial ensuring that the application functions work properly.

So, before proceeding, let us talk about an association? An association shows a relationship between two entity object’s base on common attributes. In a general term, we can say the association is a relationship between models.

I hope you already know about the related terms. So let’s do a quick review… Entity: an entity is a thing or an object in the natural world that is distinct from other objects based on the attributes.

Attribute: Entities are represented by means of their properties is called attributes.

Relationship: a relationship is where two or more tables are linked together Graphical Representation of these terms:

Let’s move on the classification of association:

One To One Association: when the every entity of the first set is related with at most one entity with another set. That is called a one to one mapping

Ex: one customer has only one identifier and one identifier belongs to only one customer

One to Many or Many to One Association: when entity of the first entity set can relate with multiple entities of another entity set. That is called one to many mapping.

Ex: every customer can have one or more cars.But if we see it other way, i.e. Many cars are associated with one customer then we can say it is a one to many relationship

Many to Many Association: when entity of the first entity set can relate to multiple entity of another entity set. And vise versa that is called many to many mapping

Ex: one employee can opt more than one department similarly one department can be opted by many employees.

MongoDB supports the one to one, one to many and many to many associations. These associations help to create two or more relations. In a mongodb there are two methods for creating a relationship

1.Embedded Relationship 2.Documented Reference Relationships

“To make a relationship between Documents. We can use reference or embedded method which should be used in general? ”

Embedding stores all related info directly inside the objects. We only use find query to find all information. And queries will run faster than if we spread them on multiple documents. It’s better to choose the embedded method for one to one and one to many. And in document reference method means “separated”. If in some cases, our documents contain references from different collection, then we have to use database reference at that time. And reference is good if you have many to many relationships because when the data become vast then identical is obligatory. And then if we want to update and document, then we have to find and update in two places.

MongoDB One to One Relationship with Example:

In this example we create two collections, customer and identifier. And collections identifier contains the whole collection of customer (using the embedded method)

MongoDB One to Many or Many to One Example:

In this example a customer can have many cars in it. But a car can have only customer. So details of cars save in the customer collection

MongoDB Many to Many Association:

In this example an employee can link many departments as well as a department can link to many employee we call them many-to-many relationships. And we will create two collections and store the related info. Department collection and employee collection

References:
https://docs.mongodb.com/manual/applications/data-models-relationships/
https://docs.mongodb.com/manual/core/document/