MongoDB-Schema Day 3 of 40 Days MERN Stack

Data in MongoDB has a flexible schema.documents in the same collection. They do not need to have the same set of fields or structure Common fields.

MongoDB-Schema Day 3 of 40 Days MERN Stack

MongoDB - Data Modelling

Data in MongoDB has a flexible schema.documents in the same collection. They do not need to have the same set of fields or structure Common fields in a collection’s documents may hold different types of data.

Data Model Design

MongoDB provides two types of data models: — Embedded data model and Normalized data model. Based on the requirement, you can use either of the models while preparing your document

  • Embedded Data Model

In this model, you can have (embed) all the related data in a single document, it is also known as de-normalized data model.

For example, assume we are getting the details of employees in three different documents namely, Personal_details, Contact and, Address, you can embed all the three documents in a single one as shown below:

{
_id: ,
Emp_ID: "10025AE336"
Personal_details:{
First_Name: "daily",
Last_Name: "tech",
Date_Of_Birth: "1995-09-26"
},
Contact: {
e-mail: "test@gmail.com",
phone: "123456789"
},
Address: {
city: "Hyderabad",
Area: "Madapur",
State: "Telangana"
}}
  • Normalized Data Model

In this model, you can refer the sub documents in the original document, using references. For example, you can re-write the above document in the normalized model as:

Employee:

{
_id: <ObjectId101>,
Emp_ID: "10025AE336"
}

Personal_details:

{
_id: <ObjectId102>,
empDocID: " ObjectId101",
First_Name: "daily",
Last_Name: "tech",
Date_Of_Birth: "1995-09-26"
}

Contact:

{
_id: <ObjectId103>,
empDocID: " ObjectId101",
e-mail: "test@gmail.com",
phone: "123456789"
}

Let’s design a Database according to client’s need.

  • Every post has the unique title, description and url.

  • Every post can have one or more tags.

  • Every post has the name of its publisher and total number of likes.

  • Every post has comments given by users along with their name, message, data-time and likes.

  • On each post, there can be zero or more comments.

Unique id , title , description and url can be written as:

{
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST
}

One or more tags can be written as:

{
tags: [TAG1, TAG2, TAG3]
}

Name of its publisher and total number of likes.

{
likes: TOTAL_LIKES,
by: POST_BY
}

Comment can be made by list of comments:

{
comments: [
{
user: 'COMMENT_BY',
message: TEXT,
datecreated: DATE_TIME,
like: LIKES
},
{
user: 'COMMENT_BY',
message: TEST,
dateCreated: DATE_TIME,
like: LIKES
}]
}

The overall schema can be designed by combining all of the above:

{
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [
{
user: 'COMMENT_BY',
message: TEXT,
datecreated: DATE_TIME,
like: LIKES
},
{
user: 'COMMENT_BY',
message: TEST,
dateCreated: DATE_TIME,
like: LIKES
}]}

Thats all for today...

Stay Tuned & Have a Good Day :)