Giter VIP home page Giter VIP logo

mongodb-association's Introduction

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.

mongodb-association's People

Contributors

subhasmitasahoo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.