Skip to content

A sample project to demonstrate Association of Entities in MongoDB

Notifications You must be signed in to change notification settings

subhasmitasahoo/MongoDB-Association

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB-Association

A sample project to demonstrate Association of Entities in MongoDB

Theory

Definition

Wiki definition
Association defines a relationship between classes of objects that allows one object instance to cause another to perform an action on its behalf.

It basically allows us to connect separate pieces of data or different entities of a complete system.There can be many types of relationship between different entities. We can group all relations into three categories:

  1. One-to-one
  2. One-to-many
  3. Many-to-many

One-to-one

Wiki definition
A one-to-one relationship is a type of cardinality that refers to the relationship between two entities (see also entity–relationship model) A and B in which one element of A may only be linked to one element of B, and vice versa.
Example
Consider Whatsapp. The relationship between your whatsapp account and status (Not the the one that expires in 24 hours). At any point of time, You have only one status and your status is yours only, not anybody else. For simplicity, assume no one else copied your status. Even though there are other ways to uniquely Identify a status.

One-to-many

Wiki definition
A one-to-many relationship is a type of cardinality that refers to the relationship between two entities (see also entity–relationship model) A and B in which an element of A may be linked to many elements of B, but a member of B is linked to only one element of A.
Example
Consider Facebook. We can divide it into multiple separate entities, such as User, Comments, Posts etc. And all of these are related to each other.
Lets consider two of them, User and Comments. Every comment is always written by a user. Hence, there is a relationsip between them. Now, lets try to know more about the nature of their relationship. A user can always have zero or more comments, but a comment is always written by only one user. Hence, it is a one to many relationship (Here, A is User and B is Comment).

Many-to-many

A many-to-many relationship is a type of cardinality that refers to the relationship between two entities A and B in which A may contain a parent instance for which there are many children in B and vice versa.
Example
Ok,lets switch back to whatsapp again.Since it is everyones favorite. Now, lets consider two entities Group and User. A user can be in multiple groups and a group can have multiple users. For example, lets say you have been currently added to three groups, "Friends Forever","We are the Badass","Laziest creatures". Taking one group into consideration, lets say "Friends Forever". In that group, you must be having many friends (unless everyone left you alone there or you created the group and didnt add anyone). Each person in the group is a user. So, your group have multiple users and at the same time you are in multiple groups. Hence, it is a many to many relationship.

Another simplest example would be Relationship between courses and students.

[You can learn more about differences in association between SQL and NoSQL Databse.](https://medium.com/@dis_is_patrick/mongodb-relations-26201385b919)

Implementation of Association in MongoDB

It can me implemented using two approaches:

  1. Embedding data

    In this approach we embed a complete document inside another document. (Compare document to tables in SQL)

  2. Referencing data

    In this approach, only the id of the document is stored inside anothee document. And using the id, the document can be referred when needed.