在 MongoDB 中,renameCollection 操作用于将一个集合重命名为另一个名称。虽然这看起来是一个简单的操作,但在不同版本的 MongoDB 中,renameCollection 有一些重大的变化和注意事项。本文将深入探讨在不同版本的 MongoDB 中执行 renameCollection 操作时需要考虑的方面。
MongoDB 3.4 版本
renameCollection is suitable for production environments; however:
renameCollection 适合生产 环境:
注意事项:
MongoDB 3.6 版本
Sharded Collections
renameCollection is not compatible with sharded collections.
Existing Target Collection
renameCollection fails if is the name of an existing collection and you do not specify .targetdropTarget: true
Performance
Changed in version 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. 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. Depending on the size of the collection, this may take longer to complete. Other operations which require exclusive access to the affected databases will be blocked until the rename completes. See What locks are taken by some common client operations? for operations which require exclusive access to all databases.
注意事项:
db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )
db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )
MongoDB 4.2 版本
Sharded Collections
renameCollection is not compatible with sharded collections.
Existing Target Collection
renameCollection fails if is the name of an existing collection and you do not specify .targetdropTarget: true
Performance
Changed in version 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. 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. Depending on the size of the collection, this may take longer to complete.
Resource Locking
Changed in version 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. All subsequent operations on the collections must wait until renameCollection completes.
Prior to MongoDB 4.2, renaming a collection within the same database with renameCollection required an exclusive database lock.
If renaming a collection between different databases, renameCollection locking behavior depends on the MongoDB version:
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. Subsequent operations on the target database must wait until renameCollection releases the exclusive database lock.
For MongoDB 4.2.1 and earlier, renameCollection obtains an exclusive (W) global lock. Subsequent operations on the mongod must wait until renameCollection releases the global lock.
For more information on locking in MongoDB, see FAQ: Concurrency.
1、分片集合不兼容:
renameCollection 操作不兼容于分片集合。这意味着无法直接对分片集合使用 renameCollection 来重命名。
2、现有目标集合:
如果要重命名的目标集合名称已经存在,并且没有指定 .targetdrop: true 参数,renameCollection 操作将失败。这是为了避免意外覆盖已存在的集合。在执行 renameCollection 操作时,需要明确指定是否要删除已存在的目标集合以便重命名成功。
3、性能:
在 MongoDB 3.6 及更高版本中,renameCollection 操作的性能取决于目标命名空间的不同情况:
4、资源锁定:
在 MongoDB 4.2 版本中,renameCollection 操作的资源锁定行为发生了变化:
注意事项:
db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )
db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )
MongoDB 5.0 版本
Sharded Collections
Starting in MongoDB 5.0, you can use the renameCollection command to change the name of a sharded collection. 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.
Existing Target Collection
renameCollection fails if target is the name of an existing collection and you do not specify dropTarget: true.
Performance
Changed in version 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. 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. Depending on the size of the collection, this may take longer to complete.
Resource Locking in Sharded Clusters
Changed in version 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.
Resource Locking in Replica Sets
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. All subsequent operations on the collections must wait until renameCollection completes.
If renaming a collection between different databases, 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. Subsequent operations on the target database must wait until renameCollection releases the exclusive database lock.
For more information on locking in MongoDB, see FAQ: Concurrency.
1、行为
2、性能:
3、资源锁定:
注意事项:
db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )
db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )
跨库重命名相当于是一个copy操作,如果集合数据量较大,执行特别耗时。目标数据库上获取独占锁(W),在源数据库上获取意向共享锁(r),在源集合上获取共享锁(S)。后续对目标数据库的操作必须等待renameCollection释放独占数据库锁阻塞操作,线上慎用。
MongoDB 6.0 、7.0版本版本renameCollection未做变更
结论:
在进行 renameCollection 操作时,请务必谨慎操作。这个操作可能会对线上系统产生重大影响,因此建议在非高峰时段进行,并且提前备份数据以防万一。谨慎的操作是确保系统稳定性和数据完整性的重要步骤,我们始终致力于提供最佳的服务和解决方案,以确保您的业务顺利运行。再次感谢您对我们的信任和支持。