- Reference >
- mongo Shell Methods >
- Collection Methods >
- db.collection.renameCollection()
db.collection.renameCollection()¶
On this page
Definition¶
- db.collection.renameCollection(target, dropTarget)¶
Renames a collection. Provides a wrapper for the renameCollection database command.
Parameter Type Description target string The new name of the collection. Enclose the string in quotes. dropTarget boolean Optional. If true, mongod drops the target of renameCollection prior to renaming the collection. The default value is false.
Example¶
Call the db.collection.renameCollection() method on a collection object. For example:
db.rrecord.renameCollection("record")
This operation will rename the rrecord collection to record. If the target name (i.e. record) is the name of an existing collection, then the operation will fail.
Limitations¶
The method has the following limitations:
- db.collection.renameCollection() cannot move a collection between databases. Use renameCollection for these rename operations.
- db.collection.renameCollection() is not supported on sharded collections.
The db.collection.renameCollection() method operates within a collection by changing the metadata associated with a given collection.
Refer to the documentation renameCollection for additional warnings and messages.
Warning
The db.collection.renameCollection() method and renameCollection command will invalidate open cursors which interrupts queries that are currently returning data.
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.