Docs Menu

Docs HomePHP Library Manual

MongoDB\Database::withOptions()

On this page

  • Definition
  • Parameters
  • Return Values
  • Errors/Exceptions
  • Example
  • See Also
MongoDB\Database::withOptions()

Returns a clone of the Database object, but with different options.

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

An array specifying the desired options.

Name
Type
Description
readConcern
MongoDB\Driver\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.

A MongoDB\Database object.

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

The following example clones an existing Database object with a new read preference:

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