MongoDBClient-selectCollection.txt 1.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
===================================
MongoDB\\Client::selectCollection()
===================================

.. default-domain:: mongodb

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

Definition
----------

16 17 18
.. phpmethod:: MongoDB\\Client::selectCollection()

   Selects a collection on the server.
19 20 21

   .. code-block:: php

22 23
      function selectCollection($databaseName, $collectionName, array $options = []): MongoDB\Collection

24
   This method has the following parameters:
25

26
   .. include:: /includes/apiargs/MongoDBClient-method-selectCollection-param.rst
27

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

30
   .. include:: /includes/apiargs/MongoDBClient-method-selectCollection-option.rst
31

32
   :returns:
33

34
      A :phpclass:`MongoDB\\Collection` object.
35

36 37 38 39 40 41
Behavior
--------

The selected collection inherits options such as read preference and type
mapping from the :phpclass:`Client <MongoDB\\Client>` object. Options may be
overridden via the ``$options`` parameter.
42 43 44 45

Example
-------

46
The following example selects the ``users`` collection in the ``demo`` database:
47 48 49 50 51 52 53 54 55

.. code-block:: php

   <?php

   $client = new MongoDB\Client;

   $collection = $client->selectCollection('demo', 'users');

56 57
The following example selects the ``users`` collection in the ``demo`` database
with a custom read preference:
58 59 60

.. code-block:: php

61 62
   <?php

63 64 65 66 67 68 69 70 71
   $client = new MongoDB\Client;

   $collection = $client->selectCollection(
       'demo',
       'users',
       [
           'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
       ]
   );
72

73 74
.. seealso::

75 76
   - :phpmethod:`MongoDB\\Collection::__construct()`
   - :phpmethod:`MongoDB\\Database::selectCollection()`