Blog Post

MongoDB – Drop database

,

The dropDatabase command drops the current database, deleting the associated data files.

Different methods to drop database

Method 1:

1. select the database which you want to delete
>use < database name >

2. Then issue the below command
>db.dropDatabase()

Method 2:

1. Define the name of the database
> var dbname=”users”

2. Issue the below command
> Mongo().getDB(dbname).dropDatabase()

{ “dropped” : “users”, “ok” : 1 }

DropDatabase-2

Dropping all the databases

*****Please make a copy before deleting anything****

The first command gives you a list of dbs ‘AA’ and ‘PP’ and the second one using cursor loop through all the database and drop the databases

>db.getMongo().getDBs()[“databases”].forEach(function(dbname){ var db1=db.getMongo().getDB(dbname.name); print (db1);});
AA
PP

> db.getMongo().getDBs()[“databases”].forEach(function(dbname){ db.getMongo().getDB(dbname.name).dropDatabase();});

> show dbs

DropDatabase-1

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating