MongoDB – Data Modelling

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.

When modeling data in Mongo, keep the following things in mind

1) -What are the needs of the application – Look at the business needs of the application and see what data and the type of data needed for the application. Based on this, ensure that the structure of the document is decided accordingly.
2) -What are data retrieval patterns – If you foresee a heavy query usage then consider the use of indexes in your data model to improve the efficiency of queries.
3) Are frequent inserts, updates and removals happening in the database? Reconsider the use of indexes or incorporate sharding if required in your data modeling design to improve the efficiency of your overall MongoDB environment.

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:{
"firstName":"Romin",
"lastName":"Irani",
"preferredFullName":"Romin Irani",
},
Contact: {
phoneNumber:"408-1234567",
emailAddress:"romin.k.irani@gmail.com
},
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:

{
"userId":"rirani",
"jobTitleName":"Developer",
}

Personal_details:

{
"userId":"rirani",
"jobTitleName":"Developer",
"firstName":"Romin",
"lastName":"Irani",
"preferredFullName":"Romin Irani"
}

Contact:

{
"userId":"rirani",
"jobTitleName":"Developer",
phoneNumber:"408-1234567",
emailAddress:"romin.k.irani@gmail.com
}

Address:

{
"userId":"rirani",
"jobTitleName":"Developer",
city: "Hyderabad",
Area: "Madapur",
State: "Telangana"
}

Considerations while designing Schema in MongoDB

  • Design your schema according to user requirements.
  • Unless there is a compelling reason to do so, favor embedding.
  • Unbounded array growth is a bad design.
  • When denormalizing your data, consider the read to write ratio of the application.
  • Combine objects into one document if you will use them together. Otherwise separate them (but make sure there should not be need of joins).
  • Do joins while write, not on read.
  • Do complex aggregation in the schema.
  • Duplicate the data (but limited) because disk space is cheap as compare to compute time.
  • Optimize your schema for most frequent use cases.

    Example

    Suppose a client needs a database design for his blog/website and see the differences between RDBMS and MongoDB schema design. Website has the following requirements.

  • Every post has the unique title, description and url.
  • Every post can have one or more tags.
  • On each post, there can be zero or more comments.
  • 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.
    In RDBMS schema, design for above requirements will have minimum three tables.
    While in MongoDB schema, design will have one collection post and the following structure −

    {
    _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,
    },
    {
    user:'COMMENT_BY',
    message: TEXT,
    dateCreated: DATE_TIME,
    }
    ]
    }

    So while showing the data, in RDBMS you need to join three tables and in MongoDB, data will be shown from one collection only.

MongoDB – Environment (Prev Lesson)
(Next Lesson) MongoDB – Create Database