MongoDBClient-selectCollection.txt 1.83 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 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 45 46 47
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.
48 49 50 51

Example
-------

52
The following example selects the ``users`` collection in the ``test`` database:
53 54 55 56 57 58 59

.. code-block:: php

   <?php

   $client = new MongoDB\Client;

60
   $collection = $client->selectCollection('test', 'users');
61

62
The following example selects the ``users`` collection in the ``test`` database
63
with a custom read preference:
64 65 66

.. code-block:: php

67 68
   <?php

69 70 71
   $client = new MongoDB\Client;

   $collection = $client->selectCollection(
72
       'test',
73 74 75 76 77
       'users',
       [
           'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
       ]
   );
78

79 80
See Also
--------
81

82 83
- :phpmethod:`MongoDB\\Collection::__construct()`
- :phpmethod:`MongoDB\\Database::selectCollection()`