- Reference >
- JavaScript Methods >
- db.collection.renameCollection()
db.collection.renameCollection()¶
-
db.collection.renameCollection()¶ db.collection.renameCollection()provides a helper for therenameCollectiondatabase command in themongoshell to rename existing collections.Parameters: - target (string) – Specifies the new name of the collection. Enclose the string in quotes.
- dropTarget (boolean) – Optional. If
true,mongodwill drop the target ofrenameCollectionprior to renaming the collection.
Call the
db.collection.renameCollection()method on a collection object, to rename a collection. Specify the new name of the collection as an argument. For example:This operation will rename the
rrecordcollection torecord. If the target name (i.e.record) is the name of an existing collection, then the operation will fail.Consider the following limitations:
db.collection.renameCollection()cannot move a collection between databases. UserenameCollectionfor these rename operations.db.collection.renameCollection()cannot operation 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
renameCollectionfor additional warnings and messages.Warning
The
db.collection.renameCollection()method andrenameCollectioncommand will invalidate open cursors which interrupts queries that are currently returning data.