在 MongoDB 中,renameCollection 操作用于将一个汇合重命名为另一个称号。只管这看起来是一个便捷的操作,但在不同版本的 MongoDB 中,renameCollection 有一些严重的变动和留意事项。本文将深化讨论在不同版本的 MongoDB 中口头 renameCollection 操作时须要思考的方面。
renameCollection is suitable for production environments; however:
renameCollection 适宜消费 环境:
留意事项:
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>If the target>
留意事项:
db.adminCommand( { renameCollection: "db1.orders", to: "db1.orders2014" } )
db.adminCommand( { renameCollection: "db1.orders", to: "db2.orders2014" } )
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>If the target>Resource Locking
Changed in version 4.2.
If renaming a collection within the same>Prior to MongoDB 4.2, renaming a collection within the same>If renaming a collection between different>For MongoDB 4.2.2 and later, renameCollection obtains an exclusive (W) lock on the target>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" } )
Sharded Collections
Starting in MongoDB 5.0, you can use the renameCollection command to change the name of a sharded collection. The target>Unsharded Collections
You can use renameCollection to rename an unsharded collection in a sharded cluster as long as the source and target>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>If the target>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>If renaming a collection between different>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监禁独占数据库锁阻塞操作,线上慎用。
论断:
在启动 renameCollection 操作时,请务必审慎操作。这个操作或者会对线上系统发生严重影响,因此倡导在非高峰时段启动,并且提早备份数据以防万一。审慎的操作是确保系统稳固性和数据完整性的关键步骤,咱们一直努力于提供最佳的服务和处置打算,以确保您的业务顺利运转。再次感谢您对咱们的信赖和支持。
本网站的文章部分内容可能来源于网络和网友发布,仅供大家学习与参考,如有侵权,请联系站长进行删除处理,不代表本网站立场,转载联系作者并注明出处:https://duobeib.com/diannaowangluoweixiu/7049.html