On this page本页内容
renameCollection
¶Changes the name of an existing collection. Specify collection names to renameCollection
in the form of a complete namespace (<database>.<collection>
).
Issue the renameCollection
command against the admin database.
The command takes the following form:
The command contains the following fields:
renameCollection |
string | The namespace of the collection to rename. The namespace is a combination of the database name and the name of the collection. |
to |
string | The new namespace of the collection. If the new namespace specifies a different database, the renameCollection command copies the collection to the new database and drops the source collection. See Naming Restrictions. |
dropTarget |
boolean | true , mongod will drop the target of renameCollection prior to renaming the collection. The default value is false . |
writeConcern |
document |
When issued on a sharded cluster, Note You cannot rename a sharded collection. You can however rename an unsharded collection in a sharded cluster. |
comment |
any |
A comment can be any valid BSON type (string, integer, object, array, etc).
|
renameCollection
is not compatible with sharded collections.
renameCollection
fails if target
is the name of an existing collection and you do not specify dropTarget: true
.
Changed in version 3.6.在版本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.
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:
renameCollection
obtains 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 until renameCollection
releases the exclusive database lock.renameCollection
obtains an exclusive (W) global lock. Subsequent operations on the mongod
must wait until renameCollection
releases the global lock.For more information on locking in MongoDB, see FAQ: Concurrency.
local
Database¶local
database, which is not replicated.local
database, which is not replicated, to a replicated database.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.
mongodump
¶A mongodump
started with --oplog
fails if a client issues the renameCollection
command during the dump process. See mongodump.--oplog
for more information.
The following example renames a collection named orders
in the test
database to orders2014
in the test
database.
The mongo
shell provides the db.collection.renameCollection()
helper for the command to rename collections within the same database. The following is equivalent to the previous example:
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. |