Web Programming TutorialsLearn Data Modelling & How to Create and Drop DBs in MongoDB

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

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.

{
   _id: POST_ID
   heading: HEADING_OF_POST, 
   publish_date: PUBLISH_DATE,
   description: DESCRIPTION_OF_POST,
   permalink: POST_URL,
   categories: [CATEGORY1, CATEGORY2, CATEGORY3, CATEGORY4],
   likes: TOTAL_NO_OF_LIKES, 
   comments: [	
      {
         name: USER_NAME,
         email: EMAIL_ID,
         phone: PHONE,
         message: MESSAGE_TEXT,
         creationDate: DATE_TIME,
         like-flag: LIKES 
      },
      {
         name: USER_NAME,
         email: EMAIL_ID,
         phone: PHONE,
         message: MESSAGE_TEXT,
         creationDate: DATE_TIME,
         like-flag: LIKES 
      }
   ]
}

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.

db.DATA_MODEL.save ({
	heading: 'Life of an awesome girl',
	publish_date: '2016-11-27',
	description: 'An awesome girl...',
	permalink: 'http://awesomeblog.com/2016/12/life-of-an-awesome-girl',
	categories: ['Make', 'Travel', 'Fiction', 'girl'],likes: 56, 
	comments: [{
		name: 'Mohit',
		email:'[email protected]',
		phone:'123456789',
		message:'awesome blog',
		creationDate:'2016-11-27',
		like_flag:true},{
		name: 'Manu',
		email:'[email protected]',
		phone:'123456789',
		message:'I like this blog very much',
		creationDate:'2016-11-27',
		like_flag:true}
                   ]
	})

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.

> use DB_NAME

Example
Let’s create DATA_MODEL in MongoDB.

> use DATA_MODEL
switched to db DATA_MODEL
>

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

> db
DATA_MODEL

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

> show dbs
local  0.000GB
test   0.000GB
DATA_MODEL 0.000GB
>

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.

> db.dropDatabase()

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.

> show dbs
local      0.000GB
DATA_MODEL 0.000GB
test       0.000GB
> db
DATA_MODEL 
> db.dropDatabase ()
{ "dropped" : "DATA_MODEL", "ok" : 1 }
> show dbs
local  0.000GB
test   0.000GB
>

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

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -