|
| 1 | +================================ |
| 2 | +MongoDB\\Client::getCollection() |
| 3 | +================================ |
| 4 | + |
| 5 | +.. contents:: On this page |
| 6 | + :local: |
| 7 | + :backlinks: none |
| 8 | + :depth: 1 |
| 9 | + :class: singlecol |
| 10 | + |
| 11 | +Definition |
| 12 | +---------- |
| 13 | + |
| 14 | +.. phpmethod:: MongoDB\Client::getCollection() |
| 15 | + |
| 16 | + Gets access to a collection on the server. This method is an alias for |
| 17 | + :phpmethod:`MongoDB\Client::selectCollection()` and will replace it in |
| 18 | + a future release. |
| 19 | + |
| 20 | + .. code-block:: php |
| 21 | + |
| 22 | + function getCollection( |
| 23 | + string $databaseName, |
| 24 | + string $collectionName, |
| 25 | + array $options = [] |
| 26 | + ): MongoDB\Collection |
| 27 | + |
| 28 | +Parameters |
| 29 | +---------- |
| 30 | + |
| 31 | +``$databaseName`` : string |
| 32 | + The name of the database containing the collection to access. |
| 33 | + |
| 34 | +``$collectionName`` : string |
| 35 | + The name of the collection to access. |
| 36 | + |
| 37 | +``$options`` : array |
| 38 | + An array specifying the desired options. |
| 39 | + |
| 40 | + .. list-table:: |
| 41 | + :header-rows: 1 |
| 42 | + :widths: 20 20 80 |
| 43 | + |
| 44 | + * - Name |
| 45 | + - Type |
| 46 | + - Description |
| 47 | + |
| 48 | + * - codec |
| 49 | + - MongoDB\\Codec\\DocumentCodec |
| 50 | + - The default :doc:`codec </data-formats/codecs>` to use for collection |
| 51 | + operations. |
| 52 | + |
| 53 | + .. versionadded:: 1.17 |
| 54 | + |
| 55 | + * - readConcern |
| 56 | + - :php:`MongoDB\Driver\ReadConcern <class.mongodb-driver-readconcern>` |
| 57 | + - The default read concern to use for collection operations. Defaults to |
| 58 | + the client's read concern. |
| 59 | + |
| 60 | + * - readPreference |
| 61 | + - :php:`MongoDB\Driver\ReadPreference <class.mongodb-driver-readpreference>` |
| 62 | + - The default read preference to use for collection operations. Defaults |
| 63 | + to the client's read preference. |
| 64 | + |
| 65 | + * - typeMap |
| 66 | + - array |
| 67 | + - The default type map to use for collection operations. Defaults to the |
| 68 | + client's type map. |
| 69 | + |
| 70 | + * - writeConcern |
| 71 | + - :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>` |
| 72 | + - The default write concern to use for collection operations. Defaults to |
| 73 | + the client's write concern. |
| 74 | + |
| 75 | +Return Values |
| 76 | +------------- |
| 77 | + |
| 78 | +A :phpclass:`MongoDB\Collection` object. |
| 79 | + |
| 80 | +Errors/Exceptions |
| 81 | +----------------- |
| 82 | + |
| 83 | +.. include:: /includes/extracts/error-invalidargumentexception.rst |
| 84 | + |
| 85 | +Behavior |
| 86 | +-------- |
| 87 | + |
| 88 | +The selected collection inherits options such as read preference and type |
| 89 | +mapping from the :phpclass:`Client <MongoDB\Client>` object. Options may be |
| 90 | +overridden by using the ``$options`` parameter. |
| 91 | + |
| 92 | +Example |
| 93 | +------- |
| 94 | + |
| 95 | +The following example gets access to the ``users`` collection in the ``test`` database: |
| 96 | + |
| 97 | +.. code-block:: php |
| 98 | + |
| 99 | + <?php |
| 100 | + |
| 101 | + $client = new MongoDB\Client; |
| 102 | + |
| 103 | + $collection = $client->getCollection('test', 'users'); |
| 104 | + |
| 105 | +The following example gets access to the ``users`` collection in the ``test`` database |
| 106 | +with a custom read preference: |
| 107 | + |
| 108 | +.. code-block:: php |
| 109 | + |
| 110 | + <?php |
| 111 | + |
| 112 | + $client = new MongoDB\Client; |
| 113 | + |
| 114 | + $collection = $client->getCollection( |
| 115 | + 'test', |
| 116 | + 'users', |
| 117 | + [ |
| 118 | + 'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'), |
| 119 | + ] |
| 120 | + ); |
| 121 | + |
| 122 | +See Also |
| 123 | +-------- |
| 124 | + |
| 125 | +- :phpmethod:`MongoDB\Collection::__construct()` |
| 126 | +- :phpmethod:`MongoDB\Database::getCollection()` |
0 commit comments