db.collection.renameCollection()
On this page本页内容
Definition定义
db.collection.renameCollection(target, dropTarget)
- Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for database commands or language-specific drivers, such as Node.js.For the database command, see the
renameCollection
command.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Renames a collection.重命名集合。Provides a wrapper for the为renameCollection
database command.renameCollection
数据库命令提供包装。Parameter参数Type类型Description描述target
string The new name of the collection.集合的新名称。Enclose the string in quotes.将字符串用引号括起来。See Naming Restrictions.请参见命名限制。dropTarget
boolean Optional.可选的。If如果为true
,mongod
drops thetarget
ofrenameCollection
prior to renaming the collection. The default value isfalse
.true
,mongod
会在重命名集合之前删除renameCollection
的target
。默认值为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
。
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 for any existing Change Streams opened on the source or target collection.db.collection.renameCollection()
方法和renameCollection
命令会为在源或目标集合上打开的任何现有更改流创建一个无效值。
The method has the following limitations:该方法有以下限制:db.collection.renameCollection()
cannot move a collection between databases.无法在数据库之间移动集合。Use对这些重命名操作使用renameCollection
for these rename operations.renameCollection
。You cannot rename views.不能重命名视图。db.collection.renameCollection()
is not supported on time series collections.在时间序列集合上不受支持。You cannot rename a collection to itself. If you try to rename a collection to itself an不能将集合重命名为其本身。如果您试图将集合重命名为其自身,则会引发IllegalOperation
error is thrown.IllegalOperation
错误。
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 在MongoDB 4.2之前,使用renameCollection
required obtaining an exclusive database lock.renameCollection
重命名同一数据库中的集合需要获得独占数据库锁。
Interaction with mongodump
与mongodump
的交互
mongodump
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
. If the target name (i.e. record
) is the name of an existing collection, then the operation will fail.rrecord
集合重命名为record
。如果目标名称(即record
)是现有集合的名称,则操作将失败。