MongoDB is a NoSQL, open-source document database that is created using C++. It is different from other Relational Databases like MySQL and is very high-performance in nature. One of the most important characteristics of NoSQL databases like MongoDB is that they have no schema. That is, unlike SQL, we do not specify a structure for the tables/databases. MongoDB uses documents and collections instead of rows and tables like SQL.

In this article, we will discuss how to create databases in MongoDB since it does not contain a “create database” option.

Master Data Science and Unlock Top-Tier Roles

With the Data Scientist Master's ProgramStart Learning
Master Data Science and Unlock Top-Tier Roles

The Use Command

Before diving into creating and deploying a database, let us understand the basics of MongoDB commands. 

The use DATABASE_NAME command in MongoDB is used to create a database if it does not exist, else, it returns to the already existing database.

Let us now see the syntax of this use command in MongoDB.

Syntax of Use Command

The use command in MongoDB is defined using the following syntax:

use DATABASE_NAME

In this, the keyword use is used to switch to the mentioned database in case it exists or create a new one if it does not.

Let us now explore a few examples to demonstrate the implementation of the above-mentioned command in MongoDB.

Example of Use Command

Given that we want to create a database with the name <database1>. 

We implement this using the use DATABASE_NAME command in MongoDB in the following way -

>use database1

switched to db database1

In order to check which database are we connected to now, we use the command db -

>db

database1

We use the show dbs command to check the complete databases list -

>show dbs

oldDB       0.87513GB

testDB      0.76524GB

Now that we have explored the use command, let us now see the complete procedure for creating a database in MongoDB.

How and When to Create a Database in MongoDB?

Before seeing how to create such a database, let us see when to actually create a database in MongoDB.

When to Create a Database in MongoDB?

A MongoDB NoSQL database is preferred over a MySQL database whenever we need to follow a rich document model to store a variety of data instead of the traditional table-based 2-dimensional row-column structured data. If this is what we need, then it is preferable to create a MongoDB database instead of a MySQL one. Now, let us explore how we do so.

Suppose we need to create a database in MongoDB called <db1>. We first need to check if this database already exists or not. To do so, we use the following command -

>show dbs

oldDB       0.87513GB

testDB      0.76524GB

We can see that the database with the name db1 does not exist. It is for us now to follow the complete procedure for successfully creating this database.

Learn Everything You Need to Know About Data!

Post Graduate Program In Data EngineeringExplore Course
Learn Everything You Need to Know About Data!

How to Create a Database In MongoDB?

To create a database using the MongoDB shell, implement the following steps -

1. Let us assume that we want to create the database called <db1> mentioned before. We check if that database exists just like we have done before using the show dbs command that lists out all the existing databases.

>show dbs

oldDB       0.87513GB

testDB      0.76524GB

2. Since <db1> does not exist, we now apply the use command implemented earlier to create and connect our database.

>use db1

switched to db db1

3. We need to use the db command to confirm creating and connecting to our database

>db

db1

4. We check the existing databases again to check if our newly created database exists -

>show dbs

oldDB       0.87513GB

testDB      0.76524GB

The reason why our newly created database is not listed here is that MongoDB actually creates and stores our database only when we insert some values or data in it. Let us see how to do that.

5. We now insert data into our database by first creating a collection. Our collection name here we are assuming to be Student, and we insert the following document in our database.

db.Student.insert(

       {

              "Studentname" : "Meena",

              "StudentBranch" : "Computer Science"

       }

)

If the document is inserted successfully, the following data is displayed:

>db.Student.insert(

…   {

…   "Studentname" : "Meena",

…   "StudentBranch" : "Computer Science"

…   }

…   )

WriteResult({ “nInserted” : 1 })

6. Run the show dbs command to now list the existing databases.

>show dbs

oldDB       0.87513GB

testDB      0.76524GB

db1           0.34281GB 

We have now successfully created and inserted data into our database in MongoDB.

Become an Expert in MongoDB With Simplilearn

In this article, we learned when and how to create a database in MongoDB in different ways. We even explored the different commands associated with creating databases in MongoDB like use, db, show, etc. 

To dive further into the world of creating and manipulating databases and becoming a successful data scientist, we recommend checking out in-depth courses and study materials. One such learning resource is Simplilearn’s Post Graduate Program in Data Science which gives comprehensive learning the various tools to level up in your data science career.