定义
参数
$options: array指定所需选项的数组。
名称类型说明builderEncoder
MongoDB\Codec\Encoder用于在查询和聚合选项中原生PHP类型序列化为BSON数据的编码器。如果省略,
Database实例将使用默认编码器。1.21版本新增。
事务外的
用于数据库操作的默认读关注。 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.
Return Values
一个 MongoDB\Database对象。
错误/异常
MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。
例子
以下示例使用新的读取偏好(read preference)克隆现有的数据库对象:
$db = (new MongoDB\Client)->test; $newDb = $db->withOptions([ 'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'), ]);