What is the MongoDB Database?

The data in MongoDB is stored in the form of BSON documents. The BSON is the type of JSON documents but it has more data types compared to JSON. All BSON documents are clubbed together into the collection and All collections are stored in databases. So MongoDB databases are used to store one or more collections of documents.

In this tutorial, we will see the creation, projection, and deletion of the MongoDB database.


MongoDB Create Database

Let's create a database named mongodb_test, but before that, let's verify the existing databases which are already present in MongoDB using the show dbs command.

Show dbs command

Use the show dbs command to check the list of databases that are already present in MongoDB.

Command:

show dbs

Output:

From the command output, we can see the following three databases are by default present.

Mongodb show dbc command


use command

The use databasename command is used to create a database in MongoDB. If the database is already created then it will switch to that database otherwise it will create a new database.

The syntax of the MongoDB use command is as below.

Syntax:

> use database_name

Command:

We will create a database name "mongodb_test" using the below command.

> use mongodb_test

Output:

The database has been created with the name "mongodb_test". We can use the db command to see the current database name.

Mongodb use database command

If we run the show dbs command then newly database will not appear because there is no document present in that. So to display the database name we have to insert at least one document in the database using the below command.

Command:

db
db.course.insert({"course_name":"cloudduggu mongodb tutorial"})
show dbs

Output:

We have inserted one document. Now if we run the show dbs command then we can see the database "mongodb_test" is showing.

Mongodb use database command


MongoDB Drop Database

Let's see the command to drop a database in MongoDB.

dropDatabase() Method

The dropDatabase() command is used to drop a selected database.

Syntax:

db.dropdatabase() 

Command:

We will use the use mongodb_test command to select the newest created database and then we will run the db.dropDatabase() command to drop the database.

> use mongodb_test
show dbs
db.dropdatabase()

Output:

Mongodb drop database command