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 key of 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.

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.

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

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.

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

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

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...