Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of PHP Library Manual, refer to the upgrade documentation.

MongoDB\Database::withOptions()

Definition

MongoDB\Database::withOptions

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

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

This method has the following parameters:

Parameter Type Description
$options array Optional. An array specifying the desired options.

The $options parameter supports the following options:

Option Type Description
readConcern MongoDB\Driver\ReadConcern Optional. The default read concern to use for database operations. Defaults to the original database’s read concern.
readPreference MongoDB\Driver\ReadPreference Optional. The default read preference to use for database operations. Defaults to the original database’s read preference.
typeMap array Optional. 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 MongoDB\Driver\WriteConcern Optional. 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:

<?php

$db = (new MongoDB\Client)->test;

$newDb = $db->withOptions([
    'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
]);