The Lone C++ Coder's Blog

The Lone C++ Coder's Blog

The continued diary of an experienced C++ programmer. Thoughts on C++ and other languages I play with, Emacs, functional, non functional and sometimes non-functioning programming.

Timo Geusch

4-Minute Read

MongoDB has a handy command to rename a collection, db.collectionName.renameCollection(). There is currently no equivalent to rename a database. Now if we accept that from time to time, one positively, absolutely just has to rename a database in MongoDB, well, there are a couple of options. Unfortunately they aren’t quite as straight forward as single MongoDB command. All methods for renaming a database in MongoDB also take a fair amount of time and/or disk space to complete. Keep this in mind when you try to use any of them.

Renaming a database using db.copyDatabase()

How does it work? You use the db.copyDatabase() command to copy the existing database into a database with the corrected, new name. After the successful copy operation, you then drop the old database and end up with a database with the correct name. An example can look like this:

> use admin
> db.copyDatabase("Activatd", "Activated")
> db.dropDatabase("Activatd")

Downsides? Well, I already mentioned the storage requirements, plus the copy operation also has to recreate all indexes on the new database. Overall, this is a pretty expensive operation. Also, you can’t use copyDatabase on a sharded cluster or via mongos. So overall, yes, you can use it to simulate renaming a database in mongoDB, but only for a very specific subset of use cases.

The documentation for db.copyDatabase() also mentions that you can also use this function to copy the database to a remote server and rename it at the same time. However, I tend to prefer the second method of copy/renaming a database, especially as it doesn’t come with a ton of caveats regarding sharded clusters.

All that said, db.copyDatabase() has been removed from MongoDB 4.2 as the underlying copydb command was removed. You can still use it if you’re running older versions of MongoDB, but I would recommend moving to the second method described below as soon as possible

Renaming a database in MongoDB using mongodump and mongorestore

This method works better when you have to rename a database in MongoDB. It is also the only recommended and available method for MongoDB 4.2 onwards. This method also works when run against a mongos and with a sharded collection. After all, mongodump/mongorestore are designed to backup any type of MongoDB database.

This method can be used to rename a database on the same server or as part of a migration to another server, replica set or sharded cluster.

How does this method work? Instead of using db.copyDatabase(), you use mongodump to dump out the database you want to rename.  You then use mongorestore to restore the dump into a new database with the corrected name, An example could look like this:

> mongodump --host local\_server --port 27017 -d Activatd --archive Activatd.dmp
> mongorestore --host remote\_server --port 27017 -d Activated --archive Activatd.dmp

The first command dumps out only the “Activatd” database using the -d Activatd command line parameter to select the database to dump. It also makes use of the –archive flag that is available since mongoDB 3.2 to write out the database dump to a single file archive format.

The mongorestore command reverts the process, takes the dump created by the first command and restores it into the database “Activated”;, thus completing the renaming process.

It’s also possible to simply pipe the output of mongodump into mongorestore and remove the need for the intermediate dump file. The same process as above would then look like this:

> mongodump --host local_server --port 27017 -d Activatd --archive | mongorestore --host remote_server --port 27017 -d Activated --archive

Note that the –archive flag isn’t specified with the optional filename parameter, which tells mongodump/mongorestore to write to and read from the standard output and standard input streams.

Disclaimer: I work for MongoDB as a Consulting Engineer. This is my personal blog and any statements, opinions, suggestions and recommendations are my own and do not reflect opinions of employers past, present and future.

Recent Posts

Categories

About

A developer's journey. Still trying to figure out this software thing after several decades.