renameCollection

On this page本页内容

Definition定义

renameCollection

Changes the name of an existing collection. 更改现有集合的名称。Specify collection names to renameCollection in the form of a complete namespace (<database>.<collection>).以完整名称空间<database>.<collection>)的形式指定要renameCollection的集合名称。

Issue the renameCollection command against the admin database.admin数据库发出renameCollection命令。

The command takes the following form:命令采用以下形式:

{ renameCollection: "<source_namespace>",
  to: "<target_namespace>",
  dropTarget: <true|false>,
  writeConcern: <document>,
  comment: <any> }

The command contains the following fields:该命令包含以下字段:

Field字段Type类型Description描述
renameCollectionstringThe namespace of the collection to rename. 要重命名的集合的命名空间The namespace is a combination of the database name and the name of the collection.命名空间是数据库名称和集合名称的组合。
tostringThe 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. 如果新名称空间指定了不同的数据库,则renameCollection命令将集合复制到新数据库并删除源集合。See Naming Restrictions.请参见命名限制
dropTargetbooleanOptional. 可选。If true, mongod will drop the target of renameCollection prior to renaming the collection. 如果为truemongod将在重命名集合之前删除renameCollectiontargetThe default value is false.默认值为false
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, mongos converts the write concern of the renameCollection command and its helper db.collection.renameCollection() to "majority".当在分片集群上发出时,mongosrenameCollection命令及其助手db.collection.renameCollection()写入关注点转换为"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:用户提供了附加到此命令的注释。设置后,此注释将与此命令的记录一起显示在以下位置:

A comment can be any valid BSON type(string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

New in version 4.4.在版本4.4中新增

Behavior行为

Sharded Collections分片集合

Starting in MongoDB 5.0, you can use the renameCollection command to change the name of a sharded collection. 从MongoDB 5.0开始,您可以使用renameCollection命令更改分片集合的名称。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.只要源数据库和目标数据库位于同一主分片上,就可以使用renameCollection重命名分片集群中的未分级集合。

Existing Target Collection现有目标集合

renameCollection fails if target is the name of an existing collection and you do not specify dropTarget: true.如果target是现有集合的名称,并且未指定dropTarget:true,则renameCollection失败。

Performance表演

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. 如果目标数据库与源数据库相同,renameCollection只需更改名称空间。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. 如果目标数据库与源数据库不同,renameCollection会将源集合中的所有文档复制到目标集合。Depending on the size of the collection, this may take longer to complete.根据集合的大小,这可能需要更长的时间才能完成。

Resource Locking in Sharded Clusters共享集群中的资源锁定

Changed in version 5.0.在版本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.有关MongoDB中锁定的更多信息,请参阅常见问题解答:并发

Resource Locking in Replica Sets副本集中的资源锁定

Changed in version 4.2.在版本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. 如果重命名同一数据库中的集合,renameCollection将在操作期间获得源集合和目标集合的独占锁定。All subsequent operations on the collections must wait until renameCollection completes.对集合的所有后续操作必须等待renameCollection完成。

Prior to MongoDB 4.2, renaming a collection within the same database with renameCollection required an exclusive database lock.在MongoDB 4.2之前,使用renameCollection重命名同一数据库中的集合需要独占数据库锁。

If renaming a collection between different databases, renameCollection locking behavior depends on the MongoDB version:如果在不同数据库之间重命名集合,renameCollection锁定行为取决于MongoDB版本:

  • For MongoDB 4.2.2 and later, 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. 对于MongoDB 4.2.2及更高版本,renameCollection获得目标数据库的独占(W)锁、源数据库的意向共享(r)锁和源集合的共享(S)锁。Subsequent operations on the target database must wait until renameCollection releases the exclusive database lock.对目标数据库的后续操作必须等待renameCollection释放独占数据库锁。
  • For MongoDB 4.2.1 and earlier, renameCollection obtains an exclusive (W) global lock. 对于MongoDB 4.2.1和更早版本,renameCollection获得一个独占(W)全局锁。Subsequent operations on the mongod must wait until renameCollection releases the global lock.mongod上的后续操作必须等待renameCollection释放全局锁。

For more information on locking in MongoDB, see FAQ: Concurrency.有关MongoDB中锁定的更多信息,请参阅常见问题解答:并发

local Database

  • You cannot rename a collection from a replicated database to the local database, which is not replicated.不能将集合从已复制数据库重命名为未复制的local数据库。
  • You cannot rename a collection from the local database, which is not replicated, to a replicated database.不能将未复制的local数据库中的集合重命名为已复制数据库。

Open Cursors打开游标

Warning警告

The db.collection.renameCollection() method and renameCollection command will invalidate open cursors which interrupts queries that are currently returning data.db.collection.renameCollection()方法和renameCollection命令将使打开的游标失效,从而中断当前返回数据的查询。

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.对于变更流db.collection.renameCollection()方法和renameCollection命令为源集合或目标集合上打开的任何现有变更流创建一个invalidate事件。

Interaction with mongodumpmongodump的交互

A mongodump started with --oplog fails if a client issues the renameCollection command during the dump process. 如果客户端在转储过程中发出renameCollection命令,以--oplog开头的mongodump将失败。See mongodump.--oplog for more information.请参阅mongodump.--oplog获取更多信息。

Example示例

The following example renames a collection named orders in the test database to orders2014 in the test database.以下示例将test数据库中名为orders的集合重命名为测试数据库中的orders2014

db.adminCommand( { renameCollection: "test.orders", to: "test.orders2014" } )

mongosh provides the db.collection.renameCollection() helper for the command to rename collections within the same database. mongosh为命令提供db.collection.renameCollection()助手,以重命名同一数据库中的集合。The following is equivalent to the previous example:以下内容与前面的示例等效:

use test
db.orders.renameCollection( "orders2014" )
←  reIndexrotateCertificates →