andy4thehuynh
andy4thehuynh

Reputation: 2172

How to remove an empty Mongo database with the same name as a populated database?

I accidentally created two databases with the same name. When I do show dbs in my Mongo shell, I get this:

> show dbs
admin (empty)
local 0.078GB
example_development (empty)
example_development 0.078GB

Is there a way to remove the empty db without tampering with the populated one?

Upvotes: 0

Views: 545

Answers (2)

zamnuts
zamnuts

Reputation: 9592

Wes Widner answer is correct in that you most likely have a non-printable character. However, rather than remove the files from the filesystem directly, I prefer a more programmatic approach. They key here is to never really call the database by its literal name, but instead keep a reference to the name in variable form:

  1. List all databases
  2. Filter those databases to match the criteria you're seeking
  3. Transform the resulting list (array) into just the database names
  4. Select the specific index of the db in question (I couldn't figure out a way to iterate through each resulting database and apply getSiblingDB to each of them).
  5. Pass that into db.getSiblingDB and issue a db.dropDatabase

Working Example

use bogus;
db.createCollection('blah');
db.getCollectionNames(); // [ "blah", "system.indexes" ]

use admin;
db.adminCommand({listDatabases:1}).databases.filter(function(d){
    return d.name === 'bogus';
}).map(function(d){
    return d.name;
}); // [ "bogus" ]


// use [0] here since we now 'bogus' was at index 0 in previous
// getSiblingDB will allow us to issue dropDatabase on the string
// representation of the database
db.getSiblingDB(db.adminCommand({listDatabases:1}).databases.filter(function(d){
    return d.name === 'bogus';
}).map(function(d){
    return d.name;
})[0]).dropDatabase();

Tailoring It to Your Problem

Instead of specifically filtering by name, we want to filter by sizeOnDisk. Be sure not to select "admin" here, so run the adminCommand with the filter/map first to get the applicable index. Chances are "admin" will be at index 0, so your database in question will most likely be at index 1:

db.getSiblingDB(db.adminCommand({listDatabases:1}).databases.filter(function(d){
    return !d.sizeOnDisk;
}).map(function(d){
    return d.name;
})[1]).dropDatabase(); // note usage of [1] here

Upvotes: 0

Wes Widner
Wes Widner

Reputation: 49

The database names aren't the same, its just that one has a non-printable character in it which mongo shell's javascript interface is suppressing for you.

In mongo its up to the driver to make sure the user is not able to submit non-standard data to the server but it has been demonstrated that BSON is remarkably flexible in what it can store.

Since you're talking about an entire database the easiest way to fix this problem would be to shutdown mongo and remove the database files. You can find where those files are with the db.serverCmdLineOpts() command.

Upvotes: 1

Related Questions