MongoDBDatabase-withOptions.txt 1.14 KB
Newer Older
1 2 3 4 5 6
================================
MongoDB\\Database::withOptions()
================================

.. default-domain:: mongodb

7 8 9 10 11 12
.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

13 14 15
Definition
----------

16
.. phpmethod:: MongoDB\\Database::withOptions()
17 18

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

20 21 22 23
   .. code-block:: php

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

24
   This method has the following parameters:
25 26 27

   .. include:: /includes/apiargs/MongoDBDatabase-method-withOptions-param.rst

28
   The ``$options`` parameter supports the following options:
29 30 31 32 33 34

   .. include:: /includes/apiargs/MongoDBDatabase-method-withOptions-option.rst

   :returns:

      A :phpclass:`MongoDB\\Database` object.
35 36 37 38

Example
-------

39 40
The following example clones an existing Database object with a new read
preference:
41 42 43 44 45

.. code-block:: php

   <?php

46
   $db = (new MongoDB\Client)->demo;
47

48
   $newDb = $db->withOptions([
49 50 51 52 53
       'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
   ]);

.. seealso::

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