For storing data in a MongoDB, you need to create a database first. It will allow you to systematically organize your data so that it can be retrieved as per requirement. If you wish to delete a database, MongoDB also allows you to delete that. In this chapter, you will learn how to create and delete a database in MongoDB.

Concept of Creating a Database in MongoDB

MongoDB has no "create" command for creating a database. Also, it is essential to note that MongoDB does not provide any specific command for creating a database. This might seem a bit agitated if you are new to this subject and database tool or in case you have used that conventional SQL as your database where you are required to create a new database, which will contain table and, you will then have to use the INSERT INTO TABLE to insert values manually within your table.

In this MongoDB tool, you need not have to produce, or it is optional to create a database manually. This is because MongoDB has the feature of automatically creating it for the first time for you, once you save your value in that collection. So, explicitly, you do not need to mention or put a command to create a database; instead it will be created automatically once the collection is filled with values.

The "use" Command for Creating Database in MongoDB

You can make use of the "use" command followed by the database_name for creating a database. This command will tell the MongoDB client to create a database by this name if there is no database exists by this name. Otherwise, this command will return the existing database that has the name.


use my_project_db


Here 'use' is the command for creating a new database in MongoDB and 'my_project_db' is the name of the database. This will prompt you with a message that it has switched to a new DB (database) name 'my_project_db'.

View the List of Databases in MongoDB

If you are eager to check the list of database that is residing with MongoDB, you can use the show dbs command. By default, it may not show your created database, and MongoDB's default database is a test.

show dbs

In order to make a list show your database name, you have to make use of the command:

Example:{"name":"Avengers: Endgame"})

Now, when you again use the show dbs command, it will now show your created database name in the list.

It is to be noted that, for checking your currently selected database, you can use the command:


Deleting a Database in MongoDB

If you are familiar with SQL, then you must have heard about the drop command. The concept of drop in SQL is used to delete the entire database or just the table, i.e., it destroys the objects like an existing database. In MongoDB, the dropDatabase command is implemented for a similar purpose. This also helps in deleting the connected data files of that database. For operating this command, you have to reside on the current database.


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

This will drop the existing database in which you are residing and will show the above message.

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram