带驱动程序的 MongoDB
定义
db.collection.renameCollection(target, dropTarget)重命名集合。为
renameCollection数据库命令提供包装器。
参数
Parameter | 类型 | 说明 |
|---|---|---|
| 字符串 | 集合的新名称。将字符串括在引号中。请参阅命名限制。 |
| 布尔 | 可选。如果 |
行为
The db.collection.renameCollection() method operates within a collection by changing the metadata associated with a given collection.
有关其他警告和消息,请参阅文档 renameCollection。
警告
The db.collection.renameCollection() method and renameCollection command will invalidate open cursors which interrupts queries that are currently returning data.
For MongoDB Change Streams, the db.collection.renameCollection() method and renameCollection command create an invalidate Event for any existing MongoDB Change Streams opened on the source or target collection.
该方法具有以下限制:
db.collection.renameCollection()cannot move a collection between databases. UserenameCollectionfor these rename operations.您无法重命名视图。
db.collection.renameCollection()is not supported on time series collections.您无法将集合重命名为其自身。如果您尝试将集合重命名为其自身,则会引发
IllegalOperation错误。
分片集群中的资源锁定
在版本5.0中进行了更改。
重命名分片集群中的分片集合或非分片集合时,源集合和目标集合都以独占方式锁定在每个分片上。对源集合和目标集合的后续操作必须等待重命名操作完成。
有关 MongoDB 中锁定的更多信息,请参阅常见问题解答:并发。
副本集中的资源锁定
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.
互动 mongodump
A mongodump started with --oplog fails if a client issues db.collection.renameCollection() during the dump process. See mongodump.--oplog for more information.
例子
Call the db.collection.renameCollection() method on a collection object. For example:
db.rrecord.renameCollection("record")
该操作会将 rrecord 集合重命名为 record。如果目标名称(即 record)是现有集合的名称,则该操作会失败。