Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB drop every database

Tags:

mongodb

I would like to know if there're a command to drop every databases from my MongoDB?

I know if I want to drop only one datatable, I just need to type the name of the database like the code below but I dont want to have to specify it.

mongo DB_NAME --eval 'db.dropDatabase();' 
like image 896
John Avatar asked Jun 16 '11 18:06

John


People also ask

How do I drop multiple databases in MongoDB?

Then you can use “show dbs” command for checking the list of Databases. Then select the database you want to delete using command “use databasename“. Then execute db. dropDatabase() command to drop an existing database.

How do I drop a database in MongoDB?

The best way to do it is from the mongodb console: > use mydb; > db. dropDatabase(); Alternatively, you can stop mongod and delete the data files from your data directory, then restart.

How do I empty a collection in MongoDB?

To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.


2 Answers

you can create a javascript loop that do the job and then execute it in the mongoconsole.

var dbs = db.getMongo().getDBNames() for(var i in dbs){     db = db.getMongo().getDB( dbs[i] );     print( "dropping db " + db.getName() );     db.dropDatabase(); } 

save it to dropall.js and then execute:

mongo dropall.js 
like image 106
ALoR Avatar answered Sep 22 '22 06:09

ALoR


Try this command:

mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){db.getSiblingDB(i).dropDatabase()})' 
like image 39
kev Avatar answered Sep 24 '22 06:09

kev