How can I rename a collection in MongoDB?

To rename a collection in MongoDB, you can use the renameCollection() method. This method allows you to change the name of an existing collection to a new name within the same database.

Syntax

db.oldCollectionName.renameCollection('newCollectionName');

Sample Data

Let us first list all the collections from database sample to see the available collections ?

use sample;
show collections;
copyThisCollectionToSampleDatabaseDemo
deleteDocuments
deleteDocumentsDemo
employee
informationAboutDelete
internalArraySizeDemo
prettyDemo
selectWhereInDemo
sourceCollection
updateInformation
userInformation

Example

Now let's change collection name 'informationAboutDelete' to 'deleteSomeInformation' ?

db.informationAboutDelete.renameCollection('deleteSomeInformation');
{ "ok" : 1 }

Verify Result

Let's check if the collection name has been successfully renamed ?

show collections;
copyThisCollectionToSampleDatabaseDemo
deleteDocuments
deleteDocumentsDemo
deleteSomeInformation
employee
internalArraySizeDemo
prettyDemo
selectWhereInDemo
sourceCollection
updateInformation
userInformation

As you can see, the collection 'informationAboutDelete' has been successfully renamed to 'deleteSomeInformation'.

Key Points

  • The renameCollection() method only works within the same database.
  • The operation returns { "ok" : 1 } upon successful completion.
  • If the target collection name already exists, the operation will fail.

Conclusion

The renameCollection() method provides a simple way to rename collections in MongoDB. Use it when you need to change collection names while preserving all the data and indexes.

Updated on: 2026-03-15T00:12:00+05:30

835 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements