MongoDBDatabase-withOptions.txt 1.26 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

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

32 33
Return Values
-------------
34

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

37 38 39 40 41
Errors/Exceptions
-----------------

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

42 43 44
Example
-------

45 46
The following example clones an existing Database object with a new read
preference:
47 48 49 50 51

.. code-block:: php

   <?php

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

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

58 59
See Also
--------
60

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