Data Modeling in MongoDB

In MongoDB, information has an adaptable diagram. It is entirely unexpected from SQL database where you needed to decide and pronounce a table's mapping before inserting information. MongoDB accumulations don't implement archive structure.

The principle challenge in information displaying is adjusting the need of the application, the execution qualities of the database motor, and the information recovery designs.

Consider the accompanying things while designing the schema in MongoDB

  • Always outline pattern as indicated by client prerequisites.
  • Do join on write operations not on read operations.
  • Objects which you need to utilize together, ought to be consolidated into one record. Else they ought to be isolated (ensure that there should not be need of joins
  • Optimize your construction for more incessant use cases.
  • Do complex aggregation in the schema.
  • You ought to copy the information however in a cutoff, on the grounds that plate space is less expensive than figure time.

For example:

Give us a chance to take an illustration of a customer who needs a database plan for his site. His site has the accompanying prerequisites:

Each post is unmistakable (contains one of a kind title, depiction and url).

Each post can have one or more labels.

Each post has the name of its distributer and aggregate number of preferences.

Every post can have zero or more remarks and the remarks must contain client name, message, information time and likes.

For the above prerequisite, at least three tables are required in RDBMS.

But in MongoDB, schema design will have one collection post and has 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,
like: LIKES
},
{
user: 'COMMENT_BY',
message: TEST,
dateCreated: DATE_TIME,
like: LIKES
}}}