How To Create a Database in MongoDB

 Create a Database in MongoDB

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.

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 -

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


database1> show dbs;

admin          192.00 KiB

config         108.00 KiB

crudoperation   48.00 KiB

local           72.00 KiB

database1>

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 -


database1> show dbs;

admin          192.00 KiB

config         108.00 KiB

crudoperation   48.00 KiB

local           72.00 KiB

database1>

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.


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 <newdatabase> 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.



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



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 -



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.



now emp collection is created.



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



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




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

Comments

Popular posts from this blog

MySQL Point in Time Recovery: How To Configure And How Does it Work?

MySQL Replication Switchover: Step-by-Step Guide

Mysql Commercial Mysqlbackup:How to take Differential or Incremental Backup and resotre using mysqlbackup utility in mysql 8.0.37 enterprise edition