- Reference >
mongoShell 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
renameCollectiondatabase command.Parameter Type Description targetstring The new name of the collection. Enclose the string in quotes. See Naming Restrictions. dropTargetboolean Optional. If true,mongoddrops the target ofrenameCollectionprior to renaming the collection. The default value isfalse.
Behavior¶
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.
For Change Streams, the
db.collection.renameCollection() method and
renameCollection command create an
invalidate Event for any existing
Change Streams opened on the source or target collection.
- The method has the following limitations:
db.collection.renameCollection()cannot move a collection between databases. UserenameCollectionfor these rename operations.db.collection.renameCollection()is not supported on sharded collections.- You cannot rename views.
Resource Locking¶
renameCollection() obtains an exclusive (W)
lock on the database and blocks other operations on the database until
it finishes.
Interaction with mongodump¶
A mongodump started with
--oplog fails if a client issues
db.collection.renameCollection() during the dump process. See
mongodump --oplog for more information.
Example¶
Call the db.collection.renameCollection() method on a
collection object. For example:
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.