Open In App

How to Rename Collection in MongoDB?

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Renaming a collection in MongoDB is a common task faced by developers and database administrators. In this article, we will learn the different methods to rename a collection in MongoDB, including the renameCollection() method and the db.runCommand() method. By the end of this article, we will have a clear understanding of how to rename a collection in MongoDB.

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 is a common task that can be easily solve using the renameCollection() method and the db.runCommand() method. These methods provide flexibility and allowing developers and database administrators to rename collections without losing any data.

When choosing between the two methods, consider the specific requirements of your application. The renameCollection() method is ideal for simple renaming operations within the same database, while the db.runCommand() method offers more flexibility and can be used to rename collections across different databases.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads