MongoDB\Database::renameCollection()
新增在版本1.10.
定义
参数
$fromCollectionName
: string- 要重命名的集合的名称。
$toCollectionName
: string- 集合的新名称。
$toDatabaseName
: string- 集合的新数据库名称。如果不指定新数据库名称,则使用当前数据库。如果新名称指定了不同的数据库,则命令将集合复制到新数据库并删除源集合。
$options
: array指定所需选项的数组。
返回值
一个数组或对象,包含 renameCollection 命令的结果文档。返回类型将取决于 typeMap
选项。
错误/异常
如果使用了不支持所选服务器的选项(例如 collation
、readConcern
、writeConcern
),则会抛出 MongoDB\Exception\UnsupportedException
。
对于与参数或选项解析相关的错误,抛出 MongoDB\Exception\InvalidArgumentException
。
对于扩展级别的其他错误(例如连接错误),抛出 MongoDB\Driver\Exception\RuntimeException。
示例
以下示例将 test
数据库中的 restaurants
集合重命名为 places
$db = (new MongoDB\Client)->test; $result = $db->renameCollection('restaurants', 'places'); var_dump($result);
输出结果将类似于
object(MongoDB\Model\BSONDocument)#8 (1) { ["storage":"ArrayObject":private]=> array(1) { ["ok"]=> float(1) } }
另请参阅
renameCollection 命令参考 MongoDB 手册