|
| 1 | +.. _php-distinct: |
| 2 | + |
| 3 | +============================== |
| 4 | +Retrieve Distinct Field Values |
| 5 | +============================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: read, unique, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+php-library+} to retrieve the |
| 24 | +distinct values of a specified field across a collection. |
| 25 | + |
| 26 | +Within a collection, different documents might contain different values for a |
| 27 | +single field. For example, one document in a ``restaurants`` collection has a |
| 28 | +``borough`` value of ``'Manhattan'``, and another has a ``borough`` value of |
| 29 | +``'Queens'``. By using the {+php-library+}, you can retrieve all the unique values |
| 30 | +that a field contains across multiple documents in a collection. |
| 31 | + |
| 32 | +Sample Data |
| 33 | +~~~~~~~~~~~ |
| 34 | + |
| 35 | +The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants`` |
| 36 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection |
| 37 | +from your PHP application, instantiate a ``MongoDB\Client`` that connects to an Atlas cluster |
| 38 | +and assign the following value to your ``collection`` variable: |
| 39 | + |
| 40 | +.. literalinclude:: /includes/read/distinct.php |
| 41 | + :language: php |
| 42 | + :dedent: |
| 43 | + :start-after: start-db-coll |
| 44 | + :end-before: end-db-coll |
| 45 | + |
| 46 | +To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 47 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 48 | + |
| 49 | +``MongoDB\Collection::distinct()`` Method |
| 50 | +----------------------------------------- |
| 51 | + |
| 52 | +To retrieve the distinct values for a specified field, call the ``MongoDB\Collection::distinct()`` |
| 53 | +method and pass in the name of the field you want to find distinct values for. |
| 54 | + |
| 55 | +Retrieve Distinct Values Across a Collection |
| 56 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +The following example retrieves the distinct values of the ``borough`` field in |
| 59 | +the ``restaurants`` collection: |
| 60 | + |
| 61 | +.. io-code-block:: |
| 62 | + :copyable: |
| 63 | + |
| 64 | + .. input:: /includes/read/distinct.php |
| 65 | + :start-after: start-distinct |
| 66 | + :end-before: end-distinct |
| 67 | + :language: php |
| 68 | + :dedent: |
| 69 | + |
| 70 | + .. output:: |
| 71 | + |
| 72 | + "Bronx" |
| 73 | + "Manhattan" |
| 74 | + "Missing" |
| 75 | + "Queens" |
| 76 | + "Staten Island" |
| 77 | + |
| 78 | +The operation returns an array that stores each distinct ``borough`` field value. Although |
| 79 | +several documents have the same value in the ``borough`` field, each value appears in the |
| 80 | +results only once. |
| 81 | + |
| 82 | +Retrieve Distinct Values Across Specified Documents |
| 83 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 84 | + |
| 85 | +You can provide a **query filter** to the ``distinct()`` method to find the distinct |
| 86 | +field values across a subset of documents in a collection. A query filter is an expression |
| 87 | +that specifies search criteria used to match documents in an operation. For more information |
| 88 | +about creating a query filter, see the :ref:`php-specify-query` guide. |
| 89 | + |
| 90 | +The following example retrieves the distinct values of the ``borough`` field for |
| 91 | +all documents that have a ``cuisine`` field value of ``'Italian'``: |
| 92 | + |
| 93 | +.. io-code-block:: |
| 94 | + :copyable: |
| 95 | + |
| 96 | + .. input:: /includes/read/distinct.php |
| 97 | + :start-after: start-distinct-with-query |
| 98 | + :end-before: end-distinct-with-query |
| 99 | + :language: php |
| 100 | + :dedent: |
| 101 | + |
| 102 | + .. output:: |
| 103 | + :visible: false |
| 104 | + |
| 105 | + "Bronx" |
| 106 | + "Manhattan" |
| 107 | + "Queens" |
| 108 | + "Staten Island" |
| 109 | + |
| 110 | +Modify Distinct Behavior |
| 111 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 112 | + |
| 113 | +You can modify the behavior of the ``distinct()`` method by passing an |
| 114 | +array that specifies option values. The following table describes some |
| 115 | +options you can set to customize the operation: |
| 116 | + |
| 117 | +.. list-table:: |
| 118 | + :widths: 30 70 |
| 119 | + :header-rows: 1 |
| 120 | + |
| 121 | + * - Option |
| 122 | + - Description |
| 123 | + |
| 124 | + * - ``collation`` |
| 125 | + - | The collation to use for the operation. |
| 126 | + | **Type**: ``array|object`` |
| 127 | + |
| 128 | + * - ``maxTimeMS`` |
| 129 | + - | The maximum amount of time in milliseconds that the operation can run. |
| 130 | + | **Type**: ``integer`` |
| 131 | + |
| 132 | + * - ``comment`` |
| 133 | + - | The comment to attach to the operation. |
| 134 | + | **Type**: any valid BSON type |
| 135 | + |
| 136 | + * - ``readPreference`` |
| 137 | + - | The read preference to use for the operation. To learn more, see |
| 138 | + :manual:`Read Preference </core/read-preference/>` in the Server manual. |
| 139 | + | **Type**: ``MongoDB\Driver\ReadPreference`` |
| 140 | + |
| 141 | +The following example retrieves the distinct values of the ``name`` field for |
| 142 | +all documents that have a ``borough`` field value of ``'Bronx'`` and a |
| 143 | +``cuisine`` field value of ``'Pizza'``. It also specifies the ``comment`` field |
| 144 | +in an options array to add a comment to the operation: |
| 145 | + |
| 146 | +.. io-code-block:: |
| 147 | + :copyable: |
| 148 | + |
| 149 | + .. input:: /includes/read/distinct.php |
| 150 | + :start-after: start-distinct-with-comment |
| 151 | + :end-before: end-distinct-with-comment |
| 152 | + :language: php |
| 153 | + :dedent: |
| 154 | + |
| 155 | + .. output:: |
| 156 | + :visible: false |
| 157 | + |
| 158 | + "$1.25 Pizza" |
| 159 | + "18 East Gunhill Pizza" |
| 160 | + "2 Bros" |
| 161 | + "Aenos Pizza" |
| 162 | + "Alitalia Pizza Restaurant" |
| 163 | + "Amici Pizza And Pasta" |
| 164 | + "Angie'S Cafe Pizza" |
| 165 | + ... |
| 166 | + |
| 167 | +API Documentation |
| 168 | +----------------- |
| 169 | + |
| 170 | +To learn more about the ``MongoDB\Collection::distinct()`` method, see the |
| 171 | +`distinct() API documentation <{+api+}/method/MongoDBCollection-distinct/>`__. |
0 commit comments