How do you rename a MongoDB database?

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?

10 s
10

Alternative solution: you can dump your db and restore that in different name. As I’ve experienced it’s much quicker than db.copyDatabase().

$ mongodump -d old_db_name -o mongodump/
$ mongorestore -d new_db_name mongodump/old_db_name

http://docs.mongodb.org/manual/tutorial/backup-with-mongodump/


This is the current official recommended approach for database renames, given that copyDatabase was removed in MongoDB 4.2:

The “copydb” command is deprecated, please use these two commands instead:

  1. mongodump (to back up data)
  2. mongorestore (to recover data from mongodump into a new namespace)

Leave a Comment