MongoDB Rename Collection

In this article, we will see how to rename a collection in MongoDB server. We can rename a collection in MongoDB server by using db.collection.renameCollection() method.

MongoDB Rename Collection Syntax:

db.collection.renameCollection(target, dropTarget)

Where
collection is the name of collection to be renamed.
target is the new name of the collection.
dropTarget is the optional parameter. If true, mongod drops the target of renameCollection prior to renaming the collection. The default value is false.

MongoDB Rename Collection examples:

1. Rename the collection ‘students’ to student in the database.

use r2schools db.students.renameCollection("student")

2. Lets verify whether collection name has been changed or not by running the command ‘show collecitons’

MongoDB Rename Collection