- Reference >
- Database Commands >
- Administration Commands >
- renameCollection
renameCollection¶
On this page
Definition¶
-
renameCollection¶ Changes the name of an existing collection. Specify collections to
renameCollectionin the form of a complete namespace, which includes the database name. Issue therenameCollectioncommand 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.dropTargetboolean Optional. If true,mongodwill drop thetargetofrenameCollectionprior to renaming the collection.renameCollectionis suitable for production environments; however:renameCollectionblocks all database activity for the duration of the operation.renameCollectionis not compatible with sharded collections.
Warning
renameCollectionfails iftargetis the name of an existing collection and you do not specifydropTarget: true.If the
renameCollectionoperation does not complete thetargetcollection and indexes will not be usable and will require manual intervention to clean up.
Exceptions¶
| exception 10026: | |
|---|---|
Raised if the source namespace does not exist. |
|
| exception 10027: | |
Raised if the target namespace exists and dropTarget is
either false or unspecified. |
|
| exception 15967: | |
Raised if the target namespace is an invalid collection
name. |
|
Shell Helper¶
The shell helper db.collection.renameCollection() provides a
simpler interface to using this command within a database.
The following is equivalent to the previous example:
Warning
You cannot use renameCollection with
sharded collections.
Warning
This command obtains a global write lock and will block other operations until it has completed.