Drop empty databases from MongoDB -
i have bunch of empty databases showing in mongodb (from time time, , unknown reason) , i'm looking easy way drop of empty ones. here piece of results of 'show dbs' command, , it's clear ones empty , not.
rs0:primary> show dbs 1442036273 (empty) 1442643016 (empty) 1443249599 (empty) <script>document (empty) <script>foo< (empty) cfide (empty) csconm (empty) cvs (empty) consolehelp (empty) controllerweb (empty) db4web (empty) mydb 0.203gb hnap1 (empty) idmprov (empty) mm (empty) mswsmtp (empty) nasapp (empty) orion (empty) ovcgi (empty) reporting (empty) saphostcontrol (empty) ...
any ideas?
use listdatabases
command array of databases have empty data, iterate on list , call dropdatabase
method on database object. following demonstrates this
> use admin > var dbs = db.admincommand("listdatabases").databases > printjson(dbs) [ { "name" : "admin", "sizeondisk" : 83886080, "empty" : false }, { "name" : "local", "sizeondisk" : 83886080, "empty" : false }, { "name" : "test", "sizeondisk" : 486539264, "empty" : false }, { "name" : "test2", "sizeondisk" : 0, "empty" : true }, { "name" : "test3", "sizeondisk" : 0, "empty" : true } ] > var emptydbs = dbs.filter(function(db){ return db.empty; }); > printjson(emptydbs) [ { "name" : "test2", "sizeondisk" : 0, "empty" : true }, { "name" : "test3", "sizeondisk" : 0, "empty" : true } ] > emptydbs.foreach(function(e){ var db = new mongo().getdb(e.name); db.dropdatabase(); })
Comments
Post a Comment