db.collection.renameCollection()

On this page本页内容

Definition定义

db.collection.renameCollection(target, dropTarget)
Important重要
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

For MongoDB API drivers, refer to the language specific MongoDB driver documentation.

Renames a collection. 重命名集合。Provides a wrapper for the renameCollection database command.renameCollection数据库命令提供包装。

Parameter参数Type类型Description描述
targetstringThe new name of the collection. 集合的新名称。Enclose the string in quotes. 用引号将字符串括起来。See Naming Restrictions.请参见命名限制
dropTargetbooleanOptional. 可选。If true, mongod drops the target of renameCollection prior to renaming the collection. 如果为truemongod将在重命名集合之前删除renameCollectiontargetThe default value is false.默认值为false

Behavior行为

The db.collection.renameCollection() method operates within a collection by changing the metadata associated with a given collection.db.collection.renameCollection()方法通过更改与给定集合关联的元数据来在集合中操作。

Refer to the documentation renameCollection for additional warnings and messages.有关其他警告和消息,请参阅文档renameCollection

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命令将使中断当前返回数据的查询的打开游标无效。

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事件

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中更改

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. 对集合的所有后续操作必须等到renameCollection()完成。Prior to MongoDB 4.2, renaming a collection within the same database with renameCollection required obtaining an exclusive database lock.在MongoDB 4.2之前,使用renameCollection重命名同一数据库中的集合需要获得独占数据库锁。

Interaction with mongodumpmongodump的交互

A mongodump started with --oplog fails if a client issues db.collection.renameCollection() during the dump process. 如果客户端在转储过程中发出db.collection.renameCollection(),以--oplog启动的mongodump将失败。See mongodump.--oplog for more information.请参阅mongodump.--oplog了解更多信息。

Example示例

Call the db.collection.renameCollection() method on a collection object. 对集合对象调用db.collection.renameCollection()方法。For example:例如:

db.rrecord.renameCollection("record")

This operation will rename the rrecord collection to record. 此操作将rrecord集合重命名为recordIf the target name (i.e. record) is the name of an existing collection, then the operation will fail.如果目标名称(即record)是现有集合的名称,则操作将失败。

←  db.collection.remove()db.collection.replaceOne() →