MongoDBDatabase-withOptions.txt 1.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
================================
MongoDB\\Database::withOptions()
================================

.. default-domain:: mongodb

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

   :phpmethod:`MongoDB\\Database::withOptions` supports the
   following parameter:
   
   .. include:: /includes/apiargs/MongoDBCollection-method-withOptions-param.rst
   
   The ``$options`` parameter supports the following options:
   
   .. include:: /includes/apiargs/MongoDBCollection-method-withOptions-option.rst

Example
-------

The following example clones the ``$newDb`` Database object with read
preference ``RP_SECONDARY``.

.. code-block:: php

   <?php
   
   $database = new MongoDB\Client;

   $newDb = $database->withOptions('sample','restaurants');

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

.. seealso::

   - :phpmethod:`MongoDB\\Collection::__construct`