Data Modeling

Data Modeling In Mongo

As the mongo have NoSQL and schema-less or flexible schema architecture we can easily model db. It does not enforce the document to follow schema so we can easily map document to object or entity. In this flexible schema means a different document in the single collection may have different schema.

In this keyof data, modeling is

  1. Structure of Documents
  2. Relationships between data.

In MongoDB relationship is represented by 2 ways.

References: In a relational database, we have a primary key and foreign key like this in MongoDB we have references in mongo means we can reference one document to another document.

Data Modeling

Embedded Data: In this, we can embed one document to another document. In this related data can store in a single document. In mongo, it is possible to add a document or array in the document.

Data Modeling

Relationship in MongoDB

  1. One to One(1:1)
  2. One to many(1: N)
  3. Many to many(M: N)
  1. One to One (1:1): In a relational database, we map primary key and a foreign key one to one mapping like this we model our database in mongo by 2 ways.
    1. Embedding document
Data Modeling

Reference: In this type, we use like a primary and foreign key which objectid of one document in the first collection given to emp_id to another collections document.

Data Modeling

One To Many (1: N): Below example one employee has many projects in his bucket. This by embedding document.

Data Modeling

db.projects.insert({“Projects”:{“Project Leader”:”Rxx”,”Project Name”:”xyz demo”},”emp_id”:ObjectId(“5cc6de12dd0c641c25ec6188”)});

db.projects.insert({“Projects”:{“Project Leader” : “Ram”,”Project Name” : “abc finance”},”emp_id”:ObjectId(“5cc6de12dd0c641c25ec6188”)});

One to Many Using Reference

Data Modeling

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *