MongoDB – Drop Database

The dropDatabase command is used to drop a database. It also deletes the associated data files. It operates on the current database. 

To delete or drop a database from MongoDB, follow these steps

  1. Select the database you want to delete with the help of USE command. Following is the syntax of USE command.use
  2. Drop the database with the help of db.dropDatabase() command. Following is the syntax of USE command.db.dropDatabase()

Example – Drop MongoDB Database

Following is an example where we shall try deleting database named codelivly.

Refer MongoDB Create Database, if you have not already created one.

Open Mongo Shell and follow the commands in sequence.

> show dbsadmin         0.000GBlocal         0.000GBtutorialkart  0.000GB> use codelivlyswitched to db codelivly> db.dropDatabase(){ "dropped" : "codelivly", "ok" : 1 }> show dbsadmin  0.000GBlocal  0.000GB>

Following is the explanation for each mongodb command we executed above

  1. show dbs  there are three databases. We shall delete codelivly database in this demonstration.
  2. use codelivly  switched to codelivly database.
  3. db.dropDatabase()  drops the database that is currently in use i.e., codelivly database.
  4. show dbs  now there are only two databases, because codelivly database is no more present.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *