Definition定义
db.collection.renameCollection(target, dropTarget)Renames a collection. Provides a wrapper for the重命名集合。为renameCollectiondatabase command.renameCollection数据库命令提供包装器。Parameter参数Type类型Description描述targetstring字符串The new name of the collection. Enclose the string in quotes. See Naming Restrictions.集合的新名称。将字符串括在引号中。请参见命名限制。dropTargetboolean布尔值Optional.可选。If如果为true,mongoddrops thetargetofrenameCollectionprior to renaming the collection. The default value isfalse.true,mongod会在重命名集合之前删除renameCollection的code>target。默认值为false。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Stable API Support稳定的API支持
Starting in MongoDB 8.1, 从MongoDB 8.1开始,renameCollection is included in Stable API V1. To use renameCollection in the Stable API, you must connect your driver to a deployment that is running MongoDB 8.1 or greater.renameCollection包含在Stable API V1中。要在Stable API中使用renameCollection,必须将驱动程序连接到运行MongoDB 8.1或更高版本的部署。
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 invalidate open cursors. db.collection.renameCollection()方法和renameCollection命令使打开的游标无效。This creates an invalidate event for any existing change streams opened on the source or target collection, and also interrupts queries that are currently returning data from the renamed collection.这将为在源集合或目标集合上打开的任何现有更改流创建一个无效事件,并中断当前从重命名的集合返回数据的查询。
The method has the following limitations:该方法有以下局限性:db.collection.renameCollection()cannot move a collection between databases. Use无法在数据库之间移动集合。对这些重命名操作使用renameCollectionfor 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您不能将集合重命名为自己。如果尝试将集合重命名为自身,则会抛出IllegalOperationerror 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副本集中的资源锁定
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()在操作期间获得源集合和目标集合的独占锁。对集合的所有后续操作都必须等到renameCollection()完成。
Interaction with mongodump与mongodump的互动
mongodumpA 如果客户端在转储过程中发出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. For example:db.collection.renameCollection()方法。例如:
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)是现有集合的名称,则操作将失败。