MongoDBCollection-withOptions.txt 1.34 KB
Newer Older
1 2 3 4 5 6
==================================
MongoDB\\Collection::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 17 18
.. phpmethod:: MongoDB\\Collection::withOptions()

   Returns a clone of the Collection object, but with different options.
19 20 21 22 23

   .. code-block:: php

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

24
   This method has the following parameters:
25

26
   .. include:: /includes/apiargs/MongoDBCollection-method-withOptions-param.rst
27

28
   The ``$options`` parameter supports the following options:
29

30 31
   .. include:: /includes/apiargs/MongoDBCollection-method-withOptions-option.rst

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

35
A :phpclass:`MongoDB\\Collection` 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 Collection object with a new read
preference:
47 48 49 50 51

.. code-block:: php

   <?php

Jeremy Mikola's avatar
Jeremy Mikola committed
52
   $collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');
53

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

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

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