================================
MongoDB\\Database::withOptions()
================================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\Database::withOptions()

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

   .. code-block:: php

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

Parameters
----------

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - readConcern
       - :php:`MongoDB\Driver\ReadConcern <class.mongodb-driver-readconcern>`
       - The default read concern to use for database operations. Defaults to
         the original database's read concern.

     * - readPreference
       - :php:`MongoDB\Driver\ReadPreference <class.mongodb-driver-readpreference>`
       - The default read preference to use for database operations. Defaults to
         the original database's read preference.

     * - typeMap
       - array
       - The :php:`type map
         <manual/en/mongodb.persistence.deserialization.php#mongodb.persistence.typemaps>`
         to apply to cursors, which determines how BSON documents are converted
         to PHP values. Defaults to the original database's type map.

     * - writeConcern
       - :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
       - The default write concern to use for database operations. Defaults to
         the original database's write concern.

Return Values
-------------

A :phpclass:`MongoDB\Database` object.

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-invalidargumentexception.rst

Example
-------

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

.. code-block:: php

   <?php

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

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

See Also
--------

- :phpmethod:`MongoDB\Database::__construct()`
