Drop empty databases from MongoDB
NickName:BoriS Ask DateTime:2015-09-30T21:24:09

Drop empty databases from MongoDB

I have a bunch of empty databases showing up in my MongoDB (from time to time, and for an unknown reason) and I'm looking for an easy way to drop all of the empty ones. Here is a piece of the results of the 'show dbs' command, and it's clear which ones are empty and which is 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?

Copyright Notice:Content Author:「BoriS」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32867307/drop-empty-databases-from-mongodb

Answers
chridam 2015-09-30T13:49:29

Use the listDatabases command to get the array of databases which have empty data, iterate over the list and call the dropDatabase method on the database object. The following demonstrates this\n\n> use admin\n> var dbs = db.adminCommand(\"listDatabases\").databases\n> printjson(dbs)\n[\n {\n \"name\" : \"admin\",\n \"sizeOnDisk\" : 83886080,\n \"empty\" : false\n },\n {\n \"name\" : \"local\",\n \"sizeOnDisk\" : 83886080,\n \"empty\" : false\n },\n {\n \"name\" : \"test\",\n \"sizeOnDisk\" : 486539264,\n \"empty\" : false\n },\n {\n \"name\" : \"test2\",\n \"sizeOnDisk\" : 0,\n \"empty\" : true\n },\n {\n \"name\" : \"test3\",\n \"sizeOnDisk\" : 0,\n \"empty\" : true\n }\n]\n> var emptyDbs = dbs.filter(function(db){ return db.empty; });\n> printjson(emptyDbs)\n[\n {\n \"name\" : \"test2\",\n \"sizeOnDisk\" : 0,\n \"empty\" : true\n },\n {\n \"name\" : \"test3\",\n \"sizeOnDisk\" : 0,\n \"empty\" : true\n }\n]\n\n> emptyDbs.forEach(function(e){\n var db = new Mongo().getDB(e.name);\n db.dropDatabase();\n})\n",


Harsh Makani 2015-09-30T13:48:03

you can create a javascript loop that do the job and then execute it in the mongoconsole.\n\nvar dbs = db.getMongo().getDBNames()\nfor(var i in dbs){\n if (db.getCollectionNames() != null) {\n continue;\n }\n db = db.getMongo().getDB( dbs[i] );\n print( \"dropping db \" + db.getName() );\n db.dropDatabase();\n}\n\n\nsave it to dropemptydbs.js and then execute:\n\nmongo dropemptydbs.js\n",


More about “Drop empty databases from MongoDB” related questions

Drop empty databases from MongoDB

I have a bunch of empty databases showing up in my MongoDB (from time to time, and for an unknown reason) and I'm looking for an easy way to drop all of the empty ones. Here is a piece of the resul...

Show Detail

PHP 7 MongoDB - drop all databases

I'm trying to unit test my mongodb methods, thus I need a tearDown() method to flush my entire mongo database after testing. Here is the function: public function tearDown() { $databases = $t...

Show Detail

Multiple MongoDb databases configuration

I need to create a large number of MongoDb databases, something around 1000+, later it will grow to more than 3000. They will be hosted on a server with SSD disks and most of the databases will have

Show Detail

Drop multiple Mongo Databases using the Node JS Driver

I am able to dropDatabase using the Node JS Driver Db class Method dropDatabase. But the problem is, I have to connect to the Database before dropping it as follows: var MongoClient = require('m...

Show Detail

How to get databases names from listDatabases in mongodb?

I am getting objects from below query in mongodb in variable $dbases = $this-&gt;connection-&gt;listDatabases(); var_dump($dbases); like object(MongoDB\Model\

Show Detail

How to drop all databases in MongoDB?

I have a list of databases in my MongoDB. How to delete all databases except local, admin, and config?

Show Detail

MongoDB permissions: restrict access to two databases

I am trying to implement some restrictions on my MongoDB server: Two databases on my server should be restricted regarding delete/drop operations - only a special user account should be allowed to...

Show Detail

mongodb select from different databases

I have about 200 mongodb databases. Every database has a collection called 'Group' and in this collection there is a field called 'meldingId'. Is it possible to make a one mongodb query which find...

Show Detail

Fastest way to reset MongoDB server / drop all databases

I am starting a test MongoDB server using MongoBox and before each test method I am resetting it: class BaseTest(unittest.TestCase): def setUp(self): self.purge_database() @

Show Detail

List all MongoDB Databases and their details from Java

I am developing a Java/MongoDB application and require a list of all existing MongoDB Databases. I know I can use this code:- final MongoClient mongoClient = DatabaseManager.getMongoclient(); fi...

Show Detail