In this chapter, we will cover the following recipes related to MongoDB administration:
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 wants to enforce some standards on the names? Relational databases do have some proprietary ways to rename the tables, and a database admin can do that for you.
This raises a question though. In the Mongo world, where collections are synonymous with tables, is there a way to rename a collection 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.
Running a MongoDB instance is what we will need for performing this collection renaming experiment. Refer to the Single node installation of MongoDB recipe in Chapter 1, Installing and Starting the MongoDB Server, for how to start the server. The operations we will be performing would be from the Mongo shell.
Let's take a look at the steps in detail:
27017
, execute the following command to connect to it from the shell:> mongo
test
database, let us create a collection with some test data. The collection we will be using is named sloppyNamedCollection
:> for(i = 0 ; i < 10 ; i++) db.sloppyNamedCollection.insert({'i':i})
sloppyNamedCollection
collection).neatNamedCollection
using the following command:> db.sloppyNamedCollection.renameCollection('neatNamedCollection') { "ok" : 1 }
sloppyNamedCollection
collection is no longer present, by executing the following command:> show collections
neatNamedCollection
collection to verify that the data that was originally in sloppyNamedCollection
is indeed present in it. Simply execute the following command 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 by 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) or not. This value defaults to false
, which means the target must not be dropped but must give an error instead. This is a sensible default, else the results would be ghastly if we accidentally gave a collection name that exists and didn't wish to drop it. If however, 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)
Now, as an exercise, try creating 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, rename it again with the second parameter as true
. This time, 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 as follows:
> db.runCommand({ renameCollection: "<source_namespace>", to: "<target_namespace>", dropTarget: <true|false> });
In our case, suppose we want to move sloppyNamedCollection
from the test
database to newDatabase
, rename it neatNamedCollection
, and drop the target database if it exists; we will execute the following command:
> db.runCommand({ renameCollection: "test.sloppyNamedCollection", to: "newDatabase.neatNamedCollection", dropTarget: true });
Also, note that the rename collection operation doesn't work on sharded collections.