Learn How Data Modelling Works in MongoDB

0
2935
Data Modelling Works

Data Modelling

In this chapter, we are going to learn about the concept of Data Modelling as well as various relationships that we can use to model data in MongoDB. Let’s quickly review the concept of data modelling, which we have already explained in the previous chapter of this tutorial series.

Concept of Data Modelling
In MongoDB, the data structure is very flexible as the schema that has documents in the same collection can hold different set of fields or structure. Also, the common fields are capable of holding different types of data in a collection’s documents. The following are the few considerations to keep in mind while we design a Schema in MongoDB.

  • First and the foremost thing is to design the schema according to the user requirements.
  • Identify the objects that can be used together and club them into one document. If they cannot be used together, then separate them and make sure that there is no need of using joins.
  • Depending on the frequency of the use cases, we should optimize our schema.
  • We can do complex aggregation in our schema.
  • In MongoDB, compute time is given higher priority over the disk space. Therefore, we can duplicate the data but up to a certain limit.
  • Joins are recommended on write operations and not on read operations.

Relationship in MongoDB
Relationship is an approach practised during data modelling which represent the way various documents are logically related to each other. There are two approaches to model relationships in MongoDB. They are Embedded and Referenced approaches of relationship. These relationships could be of 1:1 (one to one), 1:N (one to many), N:1 (many to one) or N:N (many to many) type.
Relationship in MongoDB

Example
Let’s consider an example of 1 : N (one to many) relationship, where a single blog may have multiple comments. Therefore, a single blog with two or more comments corresponds to 1 : N relationship. The following will be the document structure for blog and comment.

Document structure for a Blog

Document structure for a Comment

Embedded Data Modelling in MongoDB using one to Many Relationship
In the embedded approach to model relationship, we will embed the comment document inside the Blog document as shown below.

Advantages of Embedded Data Modelling

  • In this approach, we can maintain all the data in a single document after establishing a relationship.
  • It is easy to retrieve and requires minimum maintenance.
  • We can retrieve whole document by simply executing a single query as shown below.

Advantages of Embedded Data

Disadvantages of Embedded Data Modelling

  • When we have multiple documents to embed which keeps on growing in size, then it may deteriorate the overall read and write performance.

Referenced Data Modelling in MongoDB using one to Many Relationship
In the referenced approach to model relationship, both blog and comment documents will be maintained separately, but the blog document will contain a field which will reference the comment document’s id field as shown below.

Advantages of Referenced Data Modelling

  • In this approach, the blog document contains the array field comments which contains ObjectIds of corresponding comments.
  • We can use these ObjectIds to query the comment documents and get comment details from there.
  • It will overcome the performance issue on read and write operations caused due to a large number of embedded documents.
  • With the help of this approach, we will require to write two queries. The first query will fetch the comment ids field from the blog document and the second query will fetch the comments from comment collection as shown below.

Conclusion: –
In this chapter, we have revised the concept of data modelling in MongoDB and explained data modelling using various relationships i.e., 1:1 (one to one), 1:N (one to many), N:1 (many to one) or N:N (many to many) type in the two approaches namely embedded and referenced relationship.

LEAVE A REPLY

Please enter your comment!
Please enter your name here