Learn Data Modelling & How to Create and Drop DBs in MongoDB

0
1167
data-modelling

data-modelling

In this chapter, we are going to learn the concept of Data Modelling, create and drop DB in MongoDB. We are going to gain more knowledge as we progress in this MongoDB 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 Schema in MongoDB.

  • The First and foremost thing is to design 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 should not be any 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, the compute time is given a 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.

Let’s understand data modelling in MongoDB with the help of the following example:

Aparajita is a blogger who owns a blogging website. Her website has the following requirements. Let’s understand the differences between RDBMS and MongoDB schema design with the help of her website requirements.

  • A post in her blogging website will have the unique heading, publish date, description, permalink and total number of likes.
  • Each post could have one or more categories.
  • Each post has her name as its publisher with the public comments and the total number of likes.
  • Each post has the user comments with their name, email, phone, message, and data-timestamp.
  • Each post could have zero or more comments and likes.

The following will be the design in RDBMS (Relational Data Base Management System) schema for the above blogging website. It can have three tables with these fields as shown below.

Explanation: –

  • There are three tables i.e. comments, blog_post, and categories.
  • Each of these tables have their associated fields.
  • Table comments is related to table blog_post as many to one relationship i.e. one blog post may have multiple comments and likes.
  • Table blog_post is related to table comments as one to many relationship i.e. one blog post may have multiple categories.
  • Shown below is the diagrammatic representation of RDBMS data modelling for these three tables and their relationship among each other.

diagrammatic-representation-of-rdbms

The following will be the design in MongoDB for the above blogging website. It will have only one collection post and the following structure.

After comparing the data modelling between RDBMS and MongoDB, it is very clear that in RDBMS we require joining three tables to gather all data but in MongoDB, the data can gathered from a single collection only.

On Mongo DB, we are going to create a database DATA_MODEL where we are going to save a record as per above data model as shown below.

Output:
When we execute the above command and fetch the object using the pretty method to display formatted documents then we can see the retrieved record in pretty format as shown below.

data-model

Create DB in MongoDB
The creation of a database in MongoDB is very simple and it involves the following syntaxes.

  • The use Command: In MongoDB, the use DB_NAME is the syntax which is used to create a database. This command first checks for the database with the same name and if found then it will return the existing database, otherwise it will create a new database.

Syntax
This is the basic syntax for the use DATABASE statement.

Example
Let’s create DATA_MODEL in MongoDB.

If you want to check the current selected database the use the following command.

If you want to check our databases list, then use the command show dbs.

Drop DB in MongoDB
In MongoDB, we can use the db.dropDatabase () command to drop an existing database. The following is the complete syntax.

Syntax
The following is the basic syntax for dropping an existing database in MongoDB.

Note: The above statement will delete the currently selected database. It means that, if no database is selected and we execute this command then it will delete default ‘test’ database.

Example:
In the example, first we will show all the databases present in MongoDB and then the currently selected database i.e. DATA_MODEL. After executing the db.dropDatabase () command, DATA_MODEL database get dropped successfully which doesn’t show up when we execute show dbs command again as shown below.

Conclusion: –
In this chapter, we discussed how to do data modeling in MongoDB, various consideration to be taken while designing schema in MongoDB, and the comparison between RDBMS and MongoDB in terms of the data modelling technique for a similar blogging project requirement. Also, we have covered various syntaxes and commands which could be executed to create, and drop DB (here DAT_MODEL) in MongoDB.

LEAVE A REPLY

Please enter your comment!
Please enter your name here