MongoDBCollection-distinct.txt 5.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
===============================
MongoDB\\Collection::distinct()
===============================

.. default-domain:: mongodb

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

Definition
----------

.. phpmethod:: MongoDB\\Collection::distinct()

   Finds the distinct values for a specified field across the collection.

   .. code-block:: php

      function distinct($fieldName, $filter = [], array $options = []): mixed[]

24
   This method has the following parameters:
25 26

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

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

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

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

35
An array of the distinct values.
36

37 38 39 40 41 42 43 44
Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

45 46 47 48 49 50
Examples
--------

Return Distinct Values for a Field
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

51 52
The following example identifies the distinct values for the ``borough`` field
in the ``restaurants`` collection in the ``example`` database.
53 54

.. code-block:: php
55

56 57
   <?php

58
   $collection = (new MongoDB\Client)->example->restaurants;
59 60 61 62 63

   $distinct = $collection->distinct('borough');

   var_dump($distinct);

64 65
The output would then resemble::

66 67 68 69 70 71 72 73 74 75 76 77 78 79
   array(6) {
     [0]=>
     string(5) "Bronx"
     [1]=>
     string(8) "Brooklyn"
     [2]=>
     string(9) "Manhattan"
     [3]=>
     string(7) "Missing"
     [4]=>
     string(6) "Queens"
     [5]=>
     string(13) "Staten Island"
   }
80

81 82 83
Return Distinct Values Using a Filter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

84 85 86
The following example identifies the distinct values for the ``cuisine`` field
in the ``restaurants`` collection in the ``example`` database for documents
where the ``borough`` is ``Queens``:
87 88

.. code-block:: php
89

90 91
   <?php

92 93 94
   $collection = (new MongoDB\Client)->example->restaurants;

   $distinct = $collection->distinct('cuisine', ['borough' => 'Queens']);
95

96
   var_dump($distinct);
97 98

The output would then resemble::
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
   array(75) {
     [0]=>
     string(6) "Afghan"
     [1]=>
     string(7) "African"
     [2]=>
     string(9) "American "
     [3]=>
     string(8) "Armenian"
     [4]=>
     string(5) "Asian"
     [5]=>
     string(10) "Australian"
     [6]=>
     string(15) "Bagels/Pretzels"
     [7]=>
     string(6) "Bakery"
     [8]=>
     string(11) "Bangladeshi"
     [9]=>
     string(8) "Barbecue"
     [10]=>
     string(55) "Bottled beverages, including water, sodas, juices, etc."
     [11]=>
     string(9) "Brazilian"
     [12]=>
     string(4) "Cafe"
     [13]=>
     string(16) "Café/Coffee/Tea"
     [14]=>
     string(5) "Cajun"
     [15]=>
     string(9) "Caribbean"
     [16]=>
     string(7) "Chicken"
     [17]=>
     string(7) "Chinese"
     [18]=>
     string(13) "Chinese/Cuban"
     [19]=>
     string(16) "Chinese/Japanese"
     [20]=>
     string(11) "Continental"
     [21]=>
     string(6) "Creole"
     [22]=>
     string(5) "Czech"
     [23]=>
     string(12) "Delicatessen"
     [24]=>
     string(6) "Donuts"
     [25]=>
     string(16) "Eastern European"
     [26]=>
     string(8) "Egyptian"
     [27]=>
     string(7) "English"
     [28]=>
     string(8) "Filipino"
     [29]=>
     string(6) "French"
     [30]=>
     string(17) "Fruits/Vegetables"
     [31]=>
     string(6) "German"
     [32]=>
     string(5) "Greek"
     [33]=>
     string(10) "Hamburgers"
     [34]=>
     string(16) "Hotdogs/Pretzels"
     [35]=>
     string(31) "Ice Cream, Gelato, Yogurt, Ices"
     [36]=>
     string(6) "Indian"
     [37]=>
     string(10) "Indonesian"
     [38]=>
     string(5) "Irish"
     [39]=>
     string(7) "Italian"
     [40]=>
     string(8) "Japanese"
     [41]=>
     string(13) "Jewish/Kosher"
     [42]=>
     string(30) "Juice, Smoothies, Fruit Salads"
     [43]=>
     string(6) "Korean"
     [44]=>
     string(64) "Latin (Cuban, Dominican, Puerto Rican, South & Central American)"
     [45]=>
     string(13) "Mediterranean"
     [46]=>
     string(7) "Mexican"
     [47]=>
     string(14) "Middle Eastern"
     [48]=>
     string(8) "Moroccan"
     [49]=>
     string(25) "Not Listed/Not Applicable"
     [50]=>
     string(18) "Nuts/Confectionary"
     [51]=>
     string(5) "Other"
     [52]=>
     string(9) "Pakistani"
     [53]=>
     string(16) "Pancakes/Waffles"
     [54]=>
     string(8) "Peruvian"
     [55]=>
     string(5) "Pizza"
     [56]=>
     string(13) "Pizza/Italian"
     [57]=>
     string(6) "Polish"
     [58]=>
     string(10) "Portuguese"
     [59]=>
     string(7) "Russian"
     [60]=>
     string(6) "Salads"
     [61]=>
     string(10) "Sandwiches"
     [62]=>
     string(30) "Sandwiches/Salads/Mixed Buffet"
     [63]=>
     string(7) "Seafood"
     [64]=>
     string(9) "Soul Food"
     [65]=>
     string(18) "Soups & Sandwiches"
     [66]=>
     string(12) "Southwestern"
     [67]=>
     string(7) "Spanish"
     [68]=>
     string(5) "Steak"
     [69]=>
     string(5) "Tapas"
     [70]=>
     string(7) "Tex-Mex"
     [71]=>
     string(4) "Thai"
     [72]=>
     string(7) "Turkish"
     [73]=>
     string(10) "Vegetarian"
     [74]=>
     string(29) "Vietnamese/Cambodian/Malaysia"
   }

253 254
See Also
--------
255

256 257
- :manual:`distinct </reference/command/distinct>` command reference in the
  MongoDB manual