Create Database In MongoDB

MongoDB Create Database

Start the MongoDB service by typing this command:

net start MongoDB 

Next Now we should be working in the MongoDB shell. To run the MongoDB shell, type the following command:

mongo

Once you are in the MongoDB shell, create the database in MongoDB by typing this command:

use database_name

For example I am creating a database “employeeDB” so the command should be:

use employeeDB

Note: If the database name you mentioned is already present then this command will connect you to the database. However if the database doesn’t exist then this will create the database with the given name and connect you to it.

use employeeDB
switched to db employeeDB

At any point if you want to check the currently connected database just type the command db. This command will show the database name to which you are currently connected. This is really helpful command when you are working with several
databases so that before creating a collection or inserting a document in database, you may want to ensure that you are in the right database.

db employeeDB

To list down all the databases, use the command show dbs. This command lists down all the databases and their size on the disk.

show dbs
admin  0.000GB
local  0.000GB

As you can see that the database “employeeDB” that we have created is not present in the list of all the databases. This is because a database is not created until you save a document in it

Now we are creating a collection user and inserting a document in it.

We will learn how to create collection and document in the next tutorials.

> db.user.insert({name: "Karan", age: 30})
WriteResult({ "nInserted" : 1 })
> show dbs
admin          0.000GB
employeeDB  0.000GB
local          0.000GB

You can now see that the database “employeeDB” is created.

Leave a Reply

Your email address will not be published. Required fields are marked *