Have you ever come across a scenario where you have named a table in a relational database and at a later point of time felt that the name could have been better? Or perhaps the organization you work for was late in realizing that the table names are really getting messy and enforce some standards on the names? Relational databases do have some proprietary ways to rename the tables and a database admin would do that for you.
This raises a question though. In Mongo world, where collections are synonymous to tables, is there a way to rename a collection to some other name after it is created? In this recipe, we will explore this feature of Mongo where we rename an existing collection with some data in it.
We would need to run a MongoDB instance to perform this collection renaming experiment. Refer to the recipe Installing single node MongoDB in Chapter 1, Installing and Starting the Server for information on how to start the server. The operations we will perform would be from mongo shell.
27017
, execute the following command to connect to it from the shell:> mongo
sloppyNamedCollection. > for(i = 0 ; i < 10 ; i++) { db.sloppyNamedCollection.insert({'i':i}) };
sloppyNamedCollection
).neatNamedCollection
as follows:> db.sloppyNamedCollection.renameCollection('neatNamedCollection') { "ok" : 1 }
sloppyNamedCollection
is no longer present by executing:> show collections
neatNamedCollection
collection to verify that the data originally in sloppyNamedCollection
is indeed present in it. Simply execute the following on the mongo shell:> db.neatNamedCollection.find()
Renaming a collection is pretty simple. It is accomplished with the renameCollection
method, which takes two arguments. Generally, the function signature is as follows:
> db.<collection to rename>.renameCollection('<target name of the collection>', <drop target if exists>)
The first argument is the name to which the collection is to be renamed.
The second parameter that we didn't use is a Boolean value that tells the command whether to drop the target collection if it exists. This value defaults to false, which means do not drop the target but give an error. This is a sensible default, otherwise the results would be ghastly if we accidently gave a collection name that exists and didn't wish to drop it. However, if you know what you are doing and want the target to be dropped while renaming the collection, pass the second parameter as true. The name of this parameter is dropTarget
. In our case, the call would have been:
> db.sloppyNamedCollection.renameCollection('neatNamedCollection', true)
As an exercise, try creating the sloppyNamedCollection
again and rename it without the second parameter (or false as the value). You should see mongo complaining that the target namespace exists. Then, again rename with the second parameter as true, and now the renaming operation executes successfully.
Note that the rename operation will keep the original and the newly renamed collection in the same database. This renameCollection
method is not enough to move/rename the collection across another database. In such cases, we need to run the renameCollection
command that looks like this:
> db.runCommand({ renameCollection: "<source_namespace>", to: "<target_namespace>", dropTarget: <true|false> });
Suppose we want to rename the collection sloppyNamedCollection
to neatNamedCollection
as well as move it from test
database to newDatabase
, we can do so by executing the following command. Note the switch dropTarget: true
used is meant to remove the existing target collection (newDatabase.neatNamedCollection
) if it exists.
> db.runCommand({ renameCollection: "test.sloppyNamedCollection ", to: " newDatabase.neatNamedCollection", dropTarget: true });
Also, the rename collection operation doesn't work on sharded collections.