MongoDB Create Database & Drop Database Command

In previous articles, you have learned how to install and get started with MongoDB. In today’s article, you will learn about MongoDB Create Database & Drop database command. Before proceeding with the article, I assume that you have MongoDB installed successfully on your system and are familiar with mongo shell. I will use the mongo shell to explain how to create & drop database in MongoDB. For practice purpose, you can also start your Mongo server and start Mongo shell. If you do not remember how to do that, you can read this article on Starting MongoDBServer and Mongo Shell. Let’s proceed to article now.

MongoDB Create Database Command


Before creating any database, let’s see if you have any database existing in MongoDB or not. To check this, execute following command in Mongo shell.

show dbs

It will list out all the databases currently presented in your system.

Note: In MongoDB default database is ‘test’. If you didn’t create any database, then records (collections) will be stored in “test” database.

Now, let’s create a new database. In SQL, we create database and tables etc using create command. But there is NO create database command in MongoDB. Yes, you read it right! It might look odd but MongoDB does not provide any command to create a new database. Instead, it provides ‘USE’ command. When you write ‘USE’ command it will create a new database or open an existing database.  Confused? Let’s see the databases available in my system.

MongoDB create database & drop database command

You can see there is no database named ‘tutorialsJar’ available. Now, I am going to create a new database in MongoDB using ‘USE’ command.

use tutorialsJar

After executing this, you will see the message – ‘switched to db tutorialsJar’.

This has created a new database named ‘tutorialsJar’ (since it was not present earlier). If it was already there, it would have just opened this database.

MongoDB create database & drop database command 1

Now, in order to show database, it should have a record in the database. So, I have just created one collection in this database to show database name in the database list. Do not get confused with this, we will learn about creating collections in details later on. Here, is the  sample command that I used:

db.tutorialsJarCollection.insertOne({"type":"Blog","tutorialType":"MongoDB"});

To check the currently selected database, use the command ‘db’:

>db  

tutorialsJar

MongoDB Drop Database Command


To drop database in MongoDBDB, the dropDatabase command is used. The key point to note here is that it also deletes the associated data files. It operates on the current database.

Below is the syntax:

  1. dropDatabase() 

This syntax will delete the selected database. In case, if you have not selected (or using any) any database, it will delete default “test” database.

If you want to delete any particular database say “tutorialsJar”, use the dropDatabase() command as follows:

>use tutorialsJar // to go to tutorialsJar database

switched to db tutorialsJar
>db.dropDatabase()  

{ "dropped": "tutorialsJar", "ok": 1}

It shows that the selected database has been dropped. You can use ‘show dbs’ command to confirm that database is deleted successfully. Below is the snapshot for same.

MongoDB create database & drop database command 2

Alternatively, you could use the following command to drop the database.

>use tutorialsJar

db.runCommand( { dropDatabase:1 } )

Both commands will drop the database from the system.

Note: Software professionals can now move their programming and testing environment into the cloud having endless hosting space with hosted virtual desktop from CloudDesktopOnline.com. If you prefer a server, Rent a server at affordable prices from Apps4Rent.

That’s it in this article. I hope now you are familiar with MongoDB create database & drop database command.Don’t Forget To Share It with your friends and subscribe to our Email newsletter for more such updates. If you have any questions, please feel free to ask in the comments section below.

Mohit Arora
Follow me