MongoDB\Database::withOptions()
Definition
Parameters
$options
: arrayAn array specifying the desired options.
NameTypeDescriptionbuilderEncoder
MongoDB\Codec\Encoder
The encoder used to serialize native PHP types to BSON data in query and aggregation options. If omitted, the
Database
instance uses the default encoder.New in version 1.21.
readConcern
The default read concern to use for database operations. Defaults to the original database's read concern.
readPreference
The default read preference to use for database operations. Defaults to the original database's read preference.
typeMap
array
The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the original database's type map.
writeConcern
The default write concern to use for database operations. Defaults to the original database's write concern.
Return Values
A MongoDB\Database
object.
Errors/Exceptions
MongoDB\Exception\InvalidArgumentException
for errors related to
the parsing of parameters or options.
Example
The following example clones an existing Database object with a new read preference:
$db = (new MongoDB\Client)->test; $newDb = $db->withOptions([ 'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'), ]);