EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial Data Modeling in MongoDB
Secondary Sidebar
MongoDB Tutorial
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove

Data Modeling in MongoDB

Data Modeling in MongoDB

Introduction to Data Modeling in MongoDB

Data modeling in MongoDB is different from structured query language databases, in SQL databases we have defined the structure of database and tables but in MongoDB, there is no need to define any structure of database or tables.MongoDB data models are basically divided into two types i.e. normalized data model and embedded data model. Based on the structure and requirement we can use the data models, at the time of database creation. The main motive is to fulfill the requirement and needs of the application and characteristic performance of the database.

Syntax of Data Modeling in MongoDB

Below is the syntax of data modeling in MongoDB.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • Embedded Data Model 

{
_id: ,Field_of_document1: "",Field_of_document2: "",Field_of_document3: "", Field_of_document4: ""
},
Document1: {
Field1: "",-- Sub document 1 using embedded Model in MongoDB.
Field2: ""
Array_field1: ""
},
Document2: {
Field1: "",
Field2: "",-- Sub document 2 using embedded Model in MongoDB.
Array_field1: ""
}

  • Normalized Data Model 

Document1: {
_id (field used to reference the document): "",
Field2: ""-- Document 1 using in normalized Model in MongoDB.
Array_field1: "" }
Document2: {
User_id (Field 1): "",               -- Reference of document 1
Field2: "",                  -- Document 2 using in normalized Model in MongoDB.                                 Array_field1: ""}
Document3: {
User_id(Field 1): "",               -- Reference of document 1
Field2: "",                  -- Document 3 using in normalized Model in MongoDB.                                 Array_field1: ""}

Parameters of Data Modeling in MongoDB:

Below is the parameter description syntax in MongoDB:

  • Document 1 to Document N: We have used one or multiple documents. On the basis of type of data modeling, we have select the document in MongoDB. MongoDB data model is divided into two types.
  • Field 1 t Field N: We have used multiple fields in one document while designing a data model. We have used different fields of a different documents.
  • Array Field: We can select a field as an array while designing a data model in MongoDB.We have used different data types of the array while designing a data model in MongoDB.

How Does Data Modeling Work in MongoDB?

Below is the working of data modeling in MongoDB:

1. MongoDB document contains different types of fields and objects in the collection that we have used in MongoDB schema.

2. We need to consider the below points while designing a data model are as follows.

  • Define and design schema on the basis of application and user requirements.
  • Combine one or multiple documents or objects into a single document. If we don’t require this object in a single document then we need to separate this as per requirement.
  • Make the duplicate data of the application while implementing the data model.
  • Apply joins of write operations, don’t apply it on reading operations.
  • For better performance of the database, optimize our database schema.
  • We need to do better aggregation on the schema to improve database performance.

3. The data model in MongoDB is basically divided into two types:

Data model in MongoDB

1. Embedded Data Model

  • Embedded model has joined one or more related documents into a single document.
  • The embedded data model is very important and useful to join one or more related documents into one single document.
  • This data model allows the application to retrieve multiple related data from a single document or database operation.

2. Normalized Data Model

  • The normalized data model is also known as the reference data model. This data model is used to store the relationship including data links from one documents to multiple documents.
  • The normalized data model is very important and useful to one or more documents with each other.
  • Normalized or reference data model is consist one to one or one-to-many relationships between the documents.

4. Based on the structure and requirement we can use the data models at the time of database creation.

Examples to Implement Data Modeling

Below is an example of data model in MongoDB.

Example #1 – Embedded Data Model

Without Using the Embedded Data Model:

  • In the below example we have inserted two different documents without using any data model.
  • After inserting this document we have created an embedded data model of two different documents into one collection model.

The below example shows without using the embedded data model.

Code:

db.embedded_dataModel_brefore.insert({_id: "ABC", emp_name: "PQR"})
db.embedded_dataModel_brefore.insert({emp_id: "ABC", emp_city: "Mumbai", state: "MH", zip: "12345"})
db.embedded_dataModel_brefore.find ()

Output:

Data Modeling in MongoDB Example1

Using Embedded Data Model

In the below example, we have using the embedded data model.

  • We have to create a single document of the above two created documents. The below example shows that creating a single document of two documents using an embedded data model are as follows.

Code:

db.embedded_dataModel_after.insert({_id: "ABC", name: "PQR", address: {emp_city: "Mumbai", state: "MH", zip: "12345" }})
db.embedded_dataModel_after.find()

Output:

Data Modeling in MongoDB Example2

Example #2 – Normalized Data Model

Without Using the Normalized Data Model

  • In the below example, we have not used a normalized model to create documents. The below example shows that without using the normalized data model are as follows.
  • We have inserted two documents into the collection without taking reference of another document.

Code:

db.normalized_before.insert({book_title: "MongoDB", author_book1: "ABC", language: "English", publisher: {publisher_name: "XYZ", publication_year:2020, location: "Mumbai"}})
db.normalized_before.insert({book_title: "MongoDB1", author_book2: "PQR", language: "English", publisher: {publisher_name: "XYZ", publication_year:2020, location: "Mumbai"}})
db.normalized_before.find()

Output:

Normalized DataModel Example3

Using Normalized Data Model 

In the below example, we have using a normalized data model.

  • In the below example, we have to create three documents and giving reference to the above two documents at the time of creation.
  • In the above two documents, publisher data is repeated two times so we have a giving reference of those documents.

Code:

db.normalized_after.insert({publisher_name: "XYZ", publication_year:2020, location: "Mumbai", books_id: [100, 200]}
db.normalized_after.insert({_id:100, book_title: "MongoDB", author_book1: "ABC", language: "English" })
db.normalized_after.insert({_id:200, book_title: "MongoDB1", author_book2: "PQR", language: "English" })
db.normalized_after.find ()

Output:

Normalized DataModel Example4

Conclusion

Embedded and normalized data models are two types of data models available. In embedded data model we join related documents into single documents while in the normalized data model we implement the reference link of one or more documents. The data model is very important and useful,

Recommended Article

This is a guide to Data Modeling in MongoDB. Here we discuss how Does Data Modeling Work in MongoDB and its different types along with its examples. You can also go through our other suggested articles to learn more –

  1. How Does Collection Method Works in MongoDB?
  2. MongoDB Alternatives | Top 8
  3. Lookup in MongoDB With Characteristics
  4. MongoDB Limit() | How to Works?
Popular Course in this category
Data Modeling Course (10 Courses, 5 Projects)
  10 Online Courses |  5 Hands-on Projects |  107+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MongoDB Training Program (4 Courses, 2 Projects)4.9
Oracle Training (17 Courses, 8+ Projects)4.8
All in One Data Science Bundle (360+ Courses, 50+ projects)4.7
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more