How to change collection name in MongoDB

In this MongoDB tutorial, we will learn how to change the collection name in MongoDB. We will cover this topic with different operations and examples. These are the following topics that we are going to cover in this tutorial:

  • Change the collection name in the MongoDB
  • How to change the collection name in MongoDB Compass
  • Change the collection name using MongoDB Atlas
  • Change the collection name in MongoDB using python

Change the collection name in the MongoDB

In MongoDB, you can use the renameCollection() method to rename or change the name of an existing collection.

Syntax:

db.collection.renameCollection(target, dropTarget)

The following parameter provided by the renameCollection() method:

ParameterTypeDescription
targetstringThe new name of the collection.
dropTargetbooleanOptional, if true then mongod drops the target of renameCollection former to renaming the collection. The default value is false.

Let us understand with the help of an example.

Example:

In this example, we will learn to rename or change the name of an existing collection using the renameCollection() method.

> show collections
UUSA
Change the collection name
Change the collection name

The existing collection name “UUSA” will change by using the following query.

db.UUSA.renameCollection("United States of America")

Here, we have defined the new collection name ” United States of America” in the renameCollection() method.

Change the collection name
Change the collection name

We have successfully renamed or changed the name of the collection.

Note that, If the target name (United States of America) is the name of an existing collection then the operation will fail.

Also, check: Create index in MongoDB

How to change the collection name in MongoDB Compass

In this topic, you will learn to change or rename the collection name using the MongoDB compass. The MongoDB compass does not provide any UI element to rename the collection.

The MongoDB compass comes with an integrated MongoDB Shell. We will use the shell to change or rename the collection name. We can also perform any database command without having got to the terminal and connect to the server or cluster again.

Example:

In this example, we will rename the collection in MongoDB Compass using the MongoDB shell.

Follow the below steps:

  • Open the MongoDB compass and connect to the server.
  • Select the database where you want to rename the collection name.
  • Here, we have selected the demo database to rename the Boks collection.
  • Click on _MONGOSH.
How to change the collection name in MongoDB Compass
Select the database to change the collection name
  • Here, the MongoDB shell (MONGOSH) is used to perform any command of MongoDB.
  • The renameCollection() method is used to rename the collection name.
  • In the parameter, you have to provde new collection name.
  • The follwing query is used to rename the collection.
db.Boks.renameCollection("Books")
Change the collection name using MongoDB Compass
Change the collection name using MongoDB Compass

See, we have successfully renamed the collection name using the MongoDB compass integrated MongoDB shell.

Read: MongoDB Auto Increment ID

Change the collection name using MongoDB Atlas

The MongoDB Atlas is a cloud database that manages all the complexity of deploying, managing, and improving your deployments on the cloud service provider of your choice (AWS, Azure, and GCP).

The renameCollection() method will rename a collection to the specified new name in the parameter. Let us understand with the help of an example.

Example:

You have to follow each step without any fall to understand the example.

  • Login to the MongoDB Atlas.
  • Select the project (Test) in which you want to change the collection name.
MongoDB atlas change user password
MongoDB atlas change user password
  • Click on Connect button to connect the testCluster with the MongoDB Shell.
Change the collection name
Connect with the MongoDB Shell
  • You can also use the other options as well connect your application, connect using MongoDB Compass.
How to change the collection name using MongoDB Atlas
Connect with the MongoDB Shell
  • Here, I have selected the “I have the MongoDB shell”, If you don’t have the shell in your system then you can installed it by clicking on “I do not have the MongoDB Shell installed”.
  • After that, Run the connection string in your command line.
mongo "mongodb+srv://testcluster.iofia.mongodb.net/myFirstDatabase" --username test
Rename the collection using MongoDB Atlas
Run the connection string
  • Now, you have to enter the password to start the MongoDB shell.
  • When you have created the cluster at that time also create the user and password that password you have to enter here.
Change the collection name using MongoDB
Start the MongoDB Shell
  • We have successfully started the MongoDB shell.
  • Now, you have to select the database in which you want to rename the collection name.
use sample_airbnb
  • The renameCollection() method will used to rename the collection.
  • The following query is used to change the collection name.
db.listAndReviews.renameCollection("listingsAndReviews")
  • Let see the excution of the code.
Change the collection name using MongoDB Atlas
Change the collection name using MongoDB Atlas

We have successfully changed the collection name listAndReviews to listingsAndReviews.

Read: SocketException: Address already in use MongoDB

Change the collection name in MongoDB using python

In this topic, you will learn to change the collection name in MongoDB using Python. The Python library PyMongo provides a function called rename() that is used to rename a collection.

The rename operation fails if the new name already exists or it is an invalid collection name. Let’s understand the use of rename() function.

Syntax:

rename(new_name, session = None, **kwargs)

Parameters:

  • new_name: The new name of the collection.
  • session: A ClientSession (Optional).
  • **Kwargs: (Optional), Allow to pass keyword argument to a function.

Example:

In this example, we will create a collection and rename it. The rename() function will rename the collection name Books to Books_Info.

import pymongo

# creating a MongoClient object and connect with the host
myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017")

# accessing the database
mydb = myclient["demo"]

# accessing the collection of the database
mycol = mydb["Books_Info"]
docs = [{"id":1, "name":"The MongoDB Database"},
        {"id":2, "name":"The MongoDB Database with Python"}]
mycol.insert_many(docs)

# renaming the collection name
collect = mycol.rename("Books")

result = mydb.list_collection_names()
for collect in result:
    print(collect)

Here, we only define the new collection name Books in the rename() function parameter. The list_collection_names() method is used to return the list of the collection in the database.

We can also define one more parameter in the rename() function, “dropTarget=True” which means that if an existing collection Books existed, then the new collection would overwrite the existing collection’s data. If it is False then the new collection name entered should be unique.

Let’s see the execution of the above code:

Change the collection name in MongoDB using python
Change the collection name in MongoDB using python

We have successfully renamed or changed the collection name using Python.

Note that, the collection_name() method is deprecated in version 3.7.0 so we have to use the list_collection_names() to return the list of the collection.

You may also like to read the following MongoDB articles.

In this tutorial, you have learned about “Change the collection name in MongoDB” with different operations and examples. And we have also covered the following list of topics:

  • Change the collection name in the MongoDB
  • How to change the collection name in MongoDB Compass
  • Change the collection name using MongoDB Atlas
  • Change the collection name in MongoDB using python