Docs 菜单

Docs 主页PHP 库手册

MongoDB\数据库::withOptions()

在此页面上

  • 定义
  • 参数
  • 返回值
  • 错误/异常
  • 例子
  • 另请参阅
MongoDB\Database::withOptions()

返回数据库对象的克隆,但具有不同的选项。

function withOptions(array $options = []): MongoDB\Database
$options : array

指定所需选项的数组。

名称
类型
说明
readConcern
MongoDB\Driver\ReadConcern
用于数据库操作的默认读关注。 Defaults to the original database's read concern.
readPreference
用于数据库操作的默认读取偏好(read preference)。默认为原始数据库的读取偏好(read preference)。
typeMap
阵列
类型映射 应用于游标,决定如何将 BSON 文档转换为 PHP 值。默认为原始数据库的类型映射。
writeConcern
用于数据库操作的默认写关注(write concern)。Defaults to the original database's write concern.

一个MongoDB\Database对象。

MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。

以下示例使用新的读取偏好(read preference)克隆现有的数据库对象:

<?php
$db = (new MongoDB\Client)->test;
$newDb = $db->withOptions([
'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'),
]);
← MongoDB\Database::watch()