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