MongoDB drop every database
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
Try this command:
mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){db.getSiblingDB(i).dropDatabase()})'
You can also do this with a simple mongo command:
db.adminCommand("listDatabases").databases.forEach( function (d) {
if (d.name != "local" && d.name != "admin" && d.name != "apiomat" && d.name != "config")
db.getSiblingDB(d.name).dropDatabase();
})
Save this to drop_all_dbs.js:
var databases = db.getMongo().getDBNames()
for(var i in databases){
db = db.getMongo().getDB( databases[i] );
if(db.getName() == "admin" || db.getName() == "local"){
print("skipping db " + db.getName())
continue
}
print( "dropping db " + db.getName() );
db.dropDatabase();
}
Now you can execute:
mongo drop_all_dbs.js
and all databases (except for admin and local) will be dropped.
This answer is a copy of ALoR's one, just fix drop of system dbs
Adding to @ALoR's answer, for convenience you can put the following in ~/.mongorc.js
function dropDatabases(){
var mongo = db.getMongo();
var dbNames = mongo.getDBNames();
for (var i = 0; i < dbNames.length; i++) {
var db = mongo.getDB( dbNames[i] );
print( "Dropping database " + db.getName() + "..." );
db.dropDatabase();
}
}
Then at the mongo shell you can simply do
dropDatabases()
From the docs:
Mongo will read the .mongorc.js file from the home directory of the user invoking mongo. In the file, users can define variables, customize the mongo shell prompt, or update information that they would like updated every time they launch a shell.