Open In App

How to Rename Collection in MongoDB?

Last Updated : 18 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Renaming collections in MongoDB is a straightforward operation that helps in maintaining and organizing databases efficiently. MongoDB provides the renameCollection command and the db.collection.renameCollection() method for changing collection names within the same database.

In this article, We will learn about the How to Rename Collection in MongoDB by understanding various methods along with the examples and so on.

How to Rename Collection in MongoDB?

When working with MongoDB, there are times when we may need to rename a collection. This can be due to changes in our application's data model or for organizational purposes. Below are the methods that help us to Rename Collection in MongoDB by queries as follows:

  1. Using the renameCollection() Method
  2. Using the db.runCommand() Method

1. Using renameCollection() 

The renameCollection() method in MongoDB is used to rename an existing collection within the same database.

Syntax:

db.collection.renameCollection( newName, { dropTarget: <boolean> } )

Explanation:

  • db is the database object.
  • collection is the existing collection object.
  • newName is the new name for the collection.
  • dropTarget is an optional parameter that specifies whether to drop the target collection if it already exists. The default value is false.

Examples of renameCollection():

Suppose we have a gfg database in courses collection which stores information about various courses. Each document in the collection represents a course and contains details such as the course name, Instructor name, fees, and duration.

After creating collections in gfg database looks like:

Screenshot-2024-04-09-203544
collections

Example 1: Rename the courses collection to new_courses

Let's rename the collection name from courses collection to new_courses collection.

Query:

db.courses.renameCollection("new_courses", { dropTarget: true })

Output:

Screenshot-2024-04-09-203805
new_courses

Example 2: Rename the new_courses collection to top_courses"

Let's rename the collection name from new_courses collection to top_courses.

Query:

db.new_courses.renameCollection("top_courses", { dropTarget: true })

Output:

Screenshot-2024-04-09-203927
top_courses

Note: renameCollection cannot be used to move collections between databases.

2. Using db.runCommand()

The db.runCommand() method is a powerful tool that allows us to execute complex MongoDB commands directly from the MongoDB shell or from within your application code

Syntax:

    db.runCommand( command, options )

Explanation:

  • command is an object that specifies the command we want to run and its parameters.
  • options is an optional object that specifies additional options for the command.

Examples of db.runCommand():

Example 1: Rename the courses collection to new_courses:

Let's rename the collection name from new_courses collection to top_courses

Query:

db.getSiblingDB('admin').runCommand({ renameCollection: "gfg.courses", to: "gfg.new_courses" })

Output:

Screenshot-2024-04-09-204502
new_courses

Example 2: Rename the new_courses collection to top_courses

Let's rename the collection name from new_courses collection to top_courses

Query:

db.getSiblingDB('admin').runCommand({ renameCollection: "gfg.new_courses", to: "gfg.top_courses" })

Output:

Screenshot-2024-04-09-204642
top_courses

Conclusion

Overall, Renaming collections in MongoDB using db.collection.renameCollection() or renameCollection via runCommand() is crucial for database management. With clear syntax and simple steps, you can easily restructure your data while considering resource locking and potential performance impacts.


Next Article
Article Tags :

Similar Reads