- Reference >
- Database Commands >
- Administration Commands >
- renameCollection
renameCollection¶
On this page
Definition¶
-
renameCollection¶ Changes the name of an existing collection. Specify collection names to
renameCollectionin the form of a complete namespace (<database>.<collection>).Issue the
renameCollectioncommand against the admin database.The command takes the following form:
The command contains the following fields:
Field Type Description renameCollectionstring The namespace of the collection to rename. The namespace is a combination of the database name and the name of the collection. tostring The new namespace of the collection. If the new namespace specifies a different database, the renameCollectioncommand copies the collection to the new database and drops the source collection. See Naming Restrictions.dropTargetboolean Optional. If true,mongodwill drop thetargetofrenameCollectionprior to renaming the collection. The default value isfalse.writeConcerndocument Optional. A document that expresses the write concern for the operation. Omit to use the default write concern.
When issued on a sharded cluster,
mongosconverts the write concern of therenameCollectioncommand and its helperdb.collection.renameCollection()to"majority".commentany Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
- mongod log messages, in the
attr.command.cursor.commentfield. - Database profiler output, in the
command.commentfield. currentOpoutput, in thecommand.commentfield.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
- mongod log messages, in the
Behavior¶
Sharded Collections¶
Starting in MongoDB 5.0, you can use the renameCollection
command to change the name of a sharded collection. The target
database must be the same as the source database.
Unsharded Collections¶
You can use renameCollection to rename an unsharded
collection in a sharded cluster as long as the source and target
databases are on the same primary shard.
Existing Target Collection¶
renameCollection fails if target is the name of an existing
collection and you do not specify dropTarget: true.
Performance¶
Changed in version 3.6.
renameCollection has different performance implications depending
on the target namespace.
If the target database is the same as the source database,
renameCollection simply changes the namespace. This is a
quick operation.
If the target database differs from the source database,
renameCollection copies all documents from the source
collection to the target collection. Depending on the size of the
collection, this may take longer to complete.
Resource Locking in Sharded Clusters¶
Changed in version 5.0.
When renaming a sharded or unsharded collection in a sharded cluster, the source and target collections are exclusively locked on every shard. Subsequent operations on the source and target collections must wait until the rename operation completes.
For more information on locking in MongoDB, see FAQ: Concurrency.
Resource Locking in Replica Sets¶
Changed in version 4.2.
If renaming a collection within the same database,
renameCollection obtains an exclusive lock on
the source and target collections for the duration of the operation.
All subsequent operations on the collections must wait until
renameCollection completes.
Prior to MongoDB 4.2, renaming a collection within the same database
with renameCollection required an exclusive database lock.
If renaming a collection between different databases,
renameCollection locking behavior depends on the MongoDB
version:
- For MongoDB 4.2.2 and later,
renameCollectionobtains an exclusive (W) lock on the target database, an intent shared (r) lock on the source database, and a shared (S) lock on the source collection. Subsequent operations on the target database must wait untilrenameCollectionreleases the exclusive database lock. - For MongoDB 4.2.1 and earlier,
renameCollectionobtains an exclusive (W) global lock. Subsequent operations on themongodmust wait untilrenameCollectionreleases the global lock.
For more information on locking in MongoDB, see FAQ: Concurrency.
local Database¶
- You cannot rename a collection from a replicated database to the
localdatabase, which is not replicated. - You cannot rename a collection from the
localdatabase, which is not replicated, to a replicated database.
Open Cursors¶
Warning
The db.collection.renameCollection() method and
renameCollection command will invalidate open cursors
which interrupts queries that are currently returning data.
Change Streams¶
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.
Interaction with mongodump¶
A mongodump started with
--oplog fails if a client issues the
renameCollection command during the
dump process. See mongodump.--oplog for more information.
Example¶
The following example renames a collection named orders in the
test database to orders2014 in the test database.
mongosh provides the
db.collection.renameCollection() helper for the command to
rename collections within the same database. The following is
equivalent to the previous example: