How do you rename a MongoDB database?

Brian Hempel picture Brian Hempel · Feb 8, 2012 · Viewed 181k times · Source

There's a typo in my MongoDB database name and I'm looking to rename the database.

I can copy and delete like so...

db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();

Is there a command to rename a database?

Answer

bzmw picture bzmw · Jul 26, 2012

You could do this:

db.copyDatabase("db_to_rename","db_renamed","localhost")
use db_to_rename
db.dropDatabase();

Editorial Note: this is the same approach used in the question itself but has proven useful to others regardless.