MongoDb Tutorial

Day 12 : Drop database in MongoDB








Mongodb tutorial series

Overview

In this part of the Learn Mongo Series, we will learn how to drop a database in mongodb. We will drop the students database.

Let's drop

We can drop a database using the db.dropDatabase() command. Synax is we can select the DB to be dropped using the use <database_name> command. Now we can drop the database using the db.dropDatabase() command. Let's drop the students database.

  • Step 1 - List the DBs: firstly list all the non empty DBs using the show dbs command as shown below :

    										
    > show dbs
    activityoverload        0.203125GB
    animals 0.203125GB
    clonecs 0.203125GB
    local   0.078125GB
    test    0.203125GB
    students 				0.078125GB  
    >
    										
    									

  • Step 2 - Drop the DB: Let's drop the students db. We can drop the database using the following commands :

    										
    >use students
    switched to db students
    >db.dropDatabase()
    { "dropped" : "students", "ok" : 1 }
    >
    										
    									

  • Step 3 - List databases again : List all the DBs again to check the successful deletion of students DB as shown below :

    										
    > show dbs
    activityoverload        0.203125GB
    animals 0.203125GB
    clonecs 0.203125GB
    local   0.078125GB
    test    0.203125GB
    >
    										
    									

  • Hurray !! Mission Accomplished. We have successfully deleted the mongodb database.

Summary

In this part of learn mongo series , we learned about how we can drop a database in mongodb. We learned the following commands of mongodb :

  1. use : This command is use to either create a new database or switch to an already existing database.
  2. show dbs : This command is used to list all the non-empty mongodb databases on the console.
  3. db.dropDatabase() : This command is used to drop the current database.