Dropping all databases in a MongoDB
I needed to drop all the databases or based on a filter in MongoDB.
The easiest way for would be to run the following javascript command.
var dbs = db.getMongo().getDBNames();
dbs.forEach(function(dbName) {
dbd = db.getMongo().getDB( dbName );
print("Dropping " + dbName + " database");
db.dropDatabase();
})
If you want to actually filter on this you can always add a condition to the drop, like drop all the databases that begin with stats_ you can do like this
var dbs = db.getMongo().getDBNames();
dbs.forEach(function(dbName) {
dbd = db.getMongo().getDB( dbName );
if (dbName.startsWith("stats_")) {
print("Dropping " + dbName + " database");
db.dropDatabase();
}
})