How to Create and Drop Databases in MongoDB from CLI

MongoDB is an open-source document database and leading NoSQL database. It is a cross-platform, document-oriented database that provides, high performance, high availability, and easy scalability. It is written in C++.  It works on the concept of collection and document. I'll briefly explain the three components in this aspect.

The database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server contains multiple databases.

A collection  is referred to a group of MongoDB documents. It is equivalent to an RDBMS table. We can find a collection within a single database. Documents within a collection can have different fields.

A document is a set of key-value pairs. They have a dynamic schema, which means that documents in the same collection do not need to have the same set of fields or structure. In addition, common fields in a collection's documents may hold different types of data.


In this tutorial, I'll explain on how to create and drop database on MongoDB from the command line.

How to Create a database in MongoDB

In MongoDB, "use DATABASE_NAME" is used to create a database. This command will create a new database if it doesn't exist, otherwise, it will return the existing database.


use Database_name


> use mytestdb
switched to db mytestdb

This will create a database named "mytestdb", if it's not present and switch to that DB after creation. If it's already present, it will switch to that existing DB with this command.

To check your currently working database, use the command db.

> use mytestdb
switched to db mytestdb
> db

How to View Databases in MongoDB

To check your databases list, use the command show dbs.

show dbs
local 0.000GB

The DB which you've created is not listed here. We need to insert at least one document into it for displaying that database in the list.

>{"name":"tutorials point"})
WriteResult({ "nInserted" : 1 })
> show dbs
local 0.000GB
mytestdb 0.000GB
sahedb 0.000GB

In MongoDB default database is "test". If you didn't create any database, then collections will be stored in the test database.

How to Change to the databases in MongoDB

Any operations performed without explicitly specifying a database will be performed on the current database. Inorder to choose our required database, we need to switch to that database using the "use" command. To view your current database you can use the command "db". For example, I'm on the database "mytestdb" currently and I need to use other database "sahedb" for my purpose. This is how I verify the current status and switch to my required one.

> db
> use sahedb
switched to db sahedb
> db

How to Drop a Database in MongoDB

MongoDB  uses db.dropDatabase() command to drop an existing database.


Basic syntax of dropDatabase() command is as follows


This will delete the selected database. If you've not selected any database, then it will delete default 'test' database. In this example, I've shown on how I removed my database "sahedb".

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

We can confirm the deletion of the database by listing the current databases.

show dbs
local 0.000GB
mytestdb 0.000GB

How to Create Collection in MongoDB

In MongoDB db.createCollection(name, options) is used to create collection.


Basic syntax of createCollection() command is as follows −

db.createCollection(name, options)

In the command, a name is the name of the collection to be created. Options is a document and is used to specify the configuration of the collection like memory size and indexing.

use mytestdb
switched to db mytestdb
> db.createCollection("media")
{ "ok" : 1 }

You can check the created collection inside the database by using the command show collections.

show collections

You can even create the collections with some options as follows:

db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }

I've highlighted the options used in this collection creation.

Mostly, we don't need to create a collection. With a simple insert command, you can create collections automatically.

db.linoxide.insert({"name" : "linux"})
WriteResult({ "nInserted" : 1 })
> show collections

How to insert data into MongoDB collection

We can insert data into MongoDB collection, by using MongoDB's insert() or save() method.


The basic syntax of insert() command is as follows −


Here, I'm inserting some data to my collection "devops" inside the "sahedb".

use sahedb
switched to db sahedb
> db.devops.insert({"name" : "SAM", "age" : "25", "Designation" : "Admin"})
WriteResult({ "nInserted" : 1 })

How to Remove a Collection in MongoDB

In MongoDB, db.collection.drop() is used to drop a collection from the database. We need to run this command inside the database.


Basic syntax of drop() command is as follows


If we're trying to drop a table which isn't existing then, then it will return "false" instead of "true". In other words, drop() method will return true, if the selected collection is dropped successfully, otherwise it will return false.

How to take Backups in MongoDB

We can take backup of the MongoDB databases using the command "mongodump".  By running this command, we can backup the entire MongoDB databases and store it in a required path.

$# mongodump --out /home/FullDBbackups
2016-12-13T06:16:00.665+0000 writing mytestdb.linoxide to
2016-12-13T06:16:00.667+0000 writing to
2016-12-13T06:16:00.668+0000 writing mytestdb.mycollection to
2016-12-13T06:16:00.671+0000 writing mytestdb.mycol to
2016-12-13T06:16:00.671+0000 done dumping mytestdb.linoxide (1 document)
2016-12-13T06:16:00.674+0000 done dumping (1 document)
2016-12-13T06:16:00.676+0000 done dumping mytestdb.mycollection (0 documents)
2016-12-13T06:16:00.676+0000 done dumping mytestdb.mycol (0 documents)
2016-12-13T06:16:00.677+0000 writing sahedb.devops to
2016-12-13T06:16:00.678+0000 done dumping sahedb.devops (0 documents)

root@mongodb:/home/FullDBbackups# ll
total 16
drwxr-xr-x 4 root root 4096 Dec 13 06:16 ./
drwxr-xr-x 5 root root 4096 Dec 13 06:15 ../
drwxr-xr-x 2 root root 4096 Dec 13 06:16 mytestdb/
drwxr-xr-x 2 root root 4096 Dec 13 06:16 sahedb/

When this command is simply run without any backup directory path, then it will create the backup inside "/root/dump" by default.

If we want to backup one single database in MongoDB, we need to specify that particular database with the option "-d" or --db=(dbname).


mongodump --db=<dbname> --out BACKUP_DIRECTORY

$ mongodump -d sahedb --out /home/
2016-12-13T06:14:58.650+0000 writing sahedb.devops to
2016-12-13T06:14:58.651+0000 done dumping sahedb.devops (0 documents)

root@mongodb:/home/sahedb# ls
devops.bson devops.metadata.json

Next, we can check on how to backup a particular collection in a MongoDB database. For example, I need to backup the collection movie in the "mytestdb" database. Let's see how to achieve that.


mongodump --collection COLLECTION --db DB_NAME root@:/home/Collectionbackup# mongodump --collection movie -d mytestdb $# ls -R dump/ dump/: mytestdb dump/mytestdb: movie.bson movie.metadata.json

We need to run this command from the folder where we need our backups.

How to restore databases in MongoDB

You can restore a backup data in MongoDB using mongorestore command. This command restores all of the data from the default backup directory which is /root/dump by default.

$# mongorestore
2016-12-13T06:51:23.327+0000 using default 'dump' directory
2016-12-13T06:51:23.328+0000 building a list of dbs and collections to restore from dump dir
2016-12-13T06:51:23.333+0000 reading metadata for from dump/mytestdb/movie.metadata.json
2016-12-13T06:51:23.335+0000 restoring from dump/mytestdb/movie.bson
2016-12-13T06:51:23.351+0000 error: E11000 duplicate key error collection: index: _id_ dup key: { : ObjectId('584e363461eab97f1e215383') }
2016-12-13T06:51:23.351+0000 restoring indexes for collection from metadata
2016-12-13T06:51:23.353+0000 finished restoring (1 document)
2016-12-13T06:51:23.354+0000 done

If you need to restore a single database from the backups taken, then you can use the following option below:


$mongorestore -d <dbname to restore> <DBBackup path>

For example, I need to restore the DB "sahedb" from the backup taken for the database at /home/sahedb. This is how I can achieve that.

$# mongorestore -d sahedb /home/sahedb
2016-12-13T06:58:50.205+0000 building a list of collections to restore from /home/sahedb dir
2016-12-13T06:58:50.207+0000 reading metadata for sahedb.devops from /home/sahedb/devops.metadata.json
2016-12-13T06:58:50.207+0000 restoring sahedb.devops from /home/sahedb/devops.bson
2016-12-13T06:58:50.210+0000 restoring indexes for collection sahedb.devops from metadata
2016-12-13T06:58:50.211+0000 finished restoring sahedb.devops (0 documents)
2016-12-13T06:58:50.211+0000 done

To restore a single collection from a database in MongoDB, we can use the following command:


mongorestore -c <collection name> -d <databasename> <.bson file path for that collection>


$ mongorestore -c movie -d mytestdb /home/Collectionbackup/dump/mytestdb/movie.bson
2016-12-13T07:08:58.098+0000 checking for collection data in /home/Collectionbackup/dump/mytestdb/movie.bson
2016-12-13T07:08:58.101+0000 reading metadata for from /home/Collectionbackup/dump/mytestdb/movie.metadata.json
2016-12-13T07:08:58.102+0000 restoring from /home/Collectionbackup/dump/mytestdb/movie.bson
2016-12-13T07:08:58.167+0000 restoring indexes for collection from metadata
2016-12-13T07:08:58.168+0000 finished restoring (1 document)
2016-12-13T07:08:58.168+0000 done

I've restored the collection "movie" in the "mytestdb" from the backup taken.


Hurrah! We've learned on how to manage a database on MongoDB in this tutorial. MongoDB has several advantages compared to other relational databases. Some of them are as follows:

  • Less Schema
  • No complex joins.
  • Deep query-ability.
  • Tuning.
  • Highly scalable

I hope this tutorial is useful for you! Please post your valuable comments and suggestions on this.

Leave a Comment