On this page本页内容
renameCollection
Changes the name of an existing collection. 更改现有集合的名称。Specify collection names to 以完整名称空间(renameCollection in the form of a complete namespace (<database>.<collection>).<database>.<collection>)的形式指定要renameCollection的集合名称。
Issue the 对renameCollection command against the admin database.admin数据库发出renameCollection命令。
The command takes the following form:命令采用以下形式:
{ renameCollection: "<source_namespace>",
to: "<target_namespace>",
dropTarget: <true|false>,
writeConcern: <document>,
comment: <any> }
The command contains the following fields:该命令包含以下字段:
renameCollection | string | |
to | string | renameCollection command copies the collection to the new database and drops the source collection. renameCollection命令将集合复制到新数据库并删除源集合。 |
dropTarget | boolean | true, mongod will drop the target of renameCollection prior to renaming the collection. true,mongod将在重命名集合之前删除renameCollection的target。false.false。
|
writeConcern | document |
|
comment | any |
|
Starting in MongoDB 5.0, you can use the 从MongoDB 5.0开始,您可以使用renameCollection command to change the name of a sharded collection. renameCollection命令更改分片集合的名称。The target database must be the same as the source database.目标数据库必须与源数据库相同。
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.renameCollection重命名分片集群中的未分级集合。
如果renameCollection fails if target is the name of an existing collection and you do not specify dropTarget: true.target是现有集合的名称,并且未指定dropTarget:true,则renameCollection失败。
Changed in version 3.6.在版本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. renameCollection只需更改名称空间。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. renameCollection会将源集合中的所有文档复制到目标集合。Depending on the size of the collection, this may take longer to complete.根据集合的大小,这可能需要更长的时间才能完成。
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中锁定的更多信息,请参阅常见问题解答:并发。
Changed in version 4.2.在版本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. renameCollection将在操作期间获得源集合和目标集合的独占锁定。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 an exclusive database lock.renameCollection重命名同一数据库中的集合需要独占数据库锁。
If renaming a collection between different databases, 如果在不同数据库之间重命名集合,renameCollection locking behavior depends on the MongoDB version:renameCollection锁定行为取决于MongoDB版本:
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. renameCollection获得目标数据库的独占(W)锁、源数据库的意向共享(r)锁和源集合的共享(S)锁。renameCollection releases the exclusive database lock.renameCollection释放独占数据库锁。renameCollection obtains an exclusive (W) global lock. renameCollection获得一个独占(W)全局锁。mongod must wait until renameCollection releases the global lock.mongod上的后续操作必须等待renameCollection释放全局锁。For more information on locking in MongoDB, see FAQ: Concurrency.有关MongoDB中锁定的更多信息,请参阅常见问题解答:并发。
local Databaselocal database, which is not replicated.local数据库。local database, which is not replicated, to a replicated database.local数据库中的集合重命名为已复制数据库。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 Event for any existing Change Streams opened on the source or target collection.db.collection.renameCollection()方法和renameCollection命令为源集合或目标集合上打开的任何现有变更流创建一个invalidate事件。
mongodumpmongodump的交互A 如果客户端在转储过程中发出mongodump started with --oplog fails if a client issues the renameCollection command during the dump process. renameCollection命令,以--oplog开头的mongodump将失败。See 请参阅mongodump.--oplog for more information.mongodump.--oplog获取更多信息。
The following example renames a collection named 以下示例将orders in the test database to orders2014 in the test database.test数据库中名为orders的集合重命名为测试数据库中的orders2014。
db.adminCommand( { renameCollection: "test.orders", to: "test.orders2014" } )
mongosh provides the db.collection.renameCollection() helper for the command to rename collections within the same database. mongosh为命令提供db.collection.renameCollection()助手,以重命名同一数据库中的集合。The following is equivalent to the previous example:以下内容与前面的示例等效:
use test
db.orders.renameCollection( "orders2014" )