MongoDB Create Database Tutorial

MongoDB Create Database Tutorial from Coding Compiler. In this MongoDB tutorial, we will discuss how to create a database in MongoDB. Let’s start learning MongoDB.

MongoDB Create Database

Here you will learn about how to create MongoDB database.

The syntax for how to create a MongoDB database.

The syntax of MongoDB to create a database is as follows:


If the database does not exist, create the database, otherwise, switch to the specified database.


In the following example we created the database runoob:

> use runoob Use runoob
Switched to db runoob
> db> db
> > 

If you want to see all the databases, you can use the show dbs command:

> show dbs Show dbs
Admin 0.000GB0.000GB
Local 0.000GBLocal 0.000GB   
> > 

As you can see, the database runoob we just created is not in the list of databases. To display it, we need to insert some data into the runoob database.

> db.runoob.insert({"name":"Novice Tutorial"})DB . runoob . INSERT ({ "name" : "novice Tutorial" })
WriteResult({ "nInserted" : 1 })WriteResult ({ "nInserted" : 1 })    
> show dbs> show dbs
Local 0.078GBLocal 0.078GB   
Runoob 0.078GB0.078GB
Test 0.078GB0.078GB
> > 

The default database in MongoDB is test. If you don’t create a new database, the collection will be stored in the test database.

Note: In MongoDB, collections are only created after the content has been inserted! That is to say, after the collection (data table) is created, a document (record) is inserted and the collection is actually created.

MongoDB Delete Database

Here you will learn about how to drop the database in MongoDB.

The Syntax for how to delete a database in MongoDB 

The syntax of MongoDB to delete/drop a database is as follows:


Delete the current database, the default is test, you can use the db command to view the current database name.


In the following example, we removed the database runoob.

First, look at all the databases:

> show dbs
local   0.078GB
runoob  0.078GB
test    0.078GB

Next, we switch to the database runoob:

> use runoob
switched to db runoob

Execute the delete command:

> db.dropDatabase()
{ "dropped" : "runoob", "ok" : 1 }

Finally, we then use the show dbs command to delete the database successfully:

> show dbs
local  0.078GB
test   0.078GB

Delete collection

The format of the collection deletion syntax is as follows:


The following example removes the collection site from the runoob database:

>use runoob 
switched to db runoob
> show tables
> show tables

Related MongoDB Tutorials For Beginners

What is MongoDB Tutorial For Beginners

MongoDB Basics Tutorial

MongoDB Update Tutorial

MongoDB Query Tutorial

MongoDB Data Modelling Tutorial

MongoDB Scenarios

MongoDB Aggregation Tutorial

MongoDB Performance and Tools

What is NoSQL

MongoDB Installation Tutorial

MongoDB Concept Parsing

MongoDB Connections Tutorial

Leave a Comment