This would be a nice script to add to your .mongorc.js file:
function dropdbs(regex, force) {
force = force || false;
regex = regex || /.*/;
var n = 0;
var s = 0;
db.adminCommand( { listDatabases: 1 } ).databases.sort(function(a,b) {
return a.name>b.name;
}).forEach(function(d){
if (!String(d.name).match(regex)) {
return;
}
print(d.name + '\t' + humanSize(d.sizeOnDisk));
n++;
s += d.sizeOnDisk;
if (!force) {
return;
}
db.getSiblingDB(d.name).dropDatabase();
});
print("");
print(n+" databases dropped - " + humanSize(s));
if (!force) {
print("");
print("This is a dry run, to make it real, force it (second parameter true)");
}
}
function humanSize(bytes) {
var units = ['B', 'KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
var z = 1024;
var u = 0;
while (bytes > z) {
u++
bytes /= z;
}
return bytes.toFixed(1) + ' ' + units[u];
}
How to use
> dropdbs(/test/)
test-0ab85c35-6548-4c82-854e-172b80b2d403 - 80.0 MiB
test-f7fc8826-376f-455a-bddb-d0901b04d03c - 80.0 MiB
test-4ab4b7e3-a4f6-445f-b312-7436533001b5 - 80.0 MiB
test-12e84537-9e96-44e4-8b49-2aa11665a81b - 80.0 MiB
test-d5d3b025-3237-4745-8f5d-19aa78fcef76 - 80.0 MiB
test-28758225-4793-49c5-b594-709a3d32d606 - 80.0 MiB
test-301ea3a0-c9b3-42b8-a95c-892a99568640 - 80.0 MiB
7 databases dropped - 560.0 MiB
This is a dry run, to make it real, force it (second parameter true)To really drop your databases:
> dropdbs(/test/)
test-0ab85c35-6548-4c82-854e-172b80b2d403 - 80.0 MiB
test-f7fc8826-376f-455a-bddb-d0901b04d03c - 80.0 MiB
test-4ab4b7e3-a4f6-445f-b312-7436533001b5 - 80.0 MiB
test-12e84537-9e96-44e4-8b49-2aa11665a81b - 80.0 MiB
test-d5d3b025-3237-4745-8f5d-19aa78fcef76 - 80.0 MiB
test-28758225-4793-49c5-b594-709a3d32d606 - 80.0 MiB
test-301ea3a0-c9b3-42b8-a95c-892a99568640 - 80.0 MiB
7 databases dropped - 560.0 MiB
