对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

MongoDB\Database::withOptions()

MongoDB\Database::withOptions()

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

function withOptions(array $options = []): MongoDB\Database
$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.

一个MongoDB\Database对象。

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

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

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