1.10版本新增。
定义
参数
$fromCollectionName: 细绳- 要重命名的collection的名称。
$toCollectionName: 细绳- 集合的新名称。
$toDatabaseName: 细绳- collection的新数据库名称。如果未指定新的数据库名称,则使用当前数据库。 如果新名称指定了不同的数据库,则该命令会将collection复制到新数据库并删除源collection。
$options: array指定所需选项的数组。
名称类型说明comment
混合
dropTarget
布尔
如果
true,MongoDB 将在重命名collection之前删除目标。默认值为false。会话
与操作相关联的客户端会话。
typeMap
阵列
应用游标的类型映射,决定如何将BSON文档转换为PHP值。默认为数据库的类型映射。
这将用于返回的命令结果文档。
writeConcern
Write concern to use for the operation. Defaults to the database's write concern.
Return Values
An array or object with the result document of the renameCollection command. The return type will depend on the typeMap option.
错误/异常
MongoDB\Exception\UnsupportedException,如果所选服务器使用了选项但不支持选项(例如collation、readConcern、writeConcern)。
MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。
MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。
例子
以下示例将restaurants test数据库中的collection重命名为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 command reference in the MongoDB manual