Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of PHP Library Manual, refer to the upgrade documentation.

MongoDB\Collection::distinct()

Definition

MongoDB\Collection::distinct

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

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

This method has the following parameters:

Parameter Type Description
$fieldName string The field for which to return distinct values.
$filter array|object Optional. The filter criteria that specifies the documents from which to retrieve the distinct values.
$options array Optional. An array specifying the desired options.

The $options parameter supports the following options:

Option Type Description
collation array|object

Optional. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the locale field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.

If the collation is unspecified but the collection has a default collation, the operation uses the collation specified for the collection. If no collation is specified for the collection or for the operation, MongoDB uses the simple binary comparison used in prior versions for string comparisons.

This option is available in MongoDB 3.4+ and will result in an exception at execution time if specified for an older server version.

maxTimeMS integer Optional. The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
readConcern MongoDB\Driver\ReadConcern

Optional. Read concern to use for the operation. Defaults to the collection’s read concern.

This is not supported for server versions prior to 3.2 and will result in an exception at execution time if used.

readPreference MongoDB\Driver\ReadPreference Optional. Read preference to use for the operation. Defaults to the collection’s read preference.

Return Values

An array of the distinct values.

Errors/Exceptions

MongoDB\Exception\UnexpectedValueException if the command response from the server was malformed.

MongoDB\Exception\UnsupportedException if options are used and not supported by the selected server (e.g. collation, readConcern, writeConcern).

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).

Behavior

When evaluating query criteria, MongoDB compares types and values according to its own comparison rules for BSON types, which differs from PHP’s comparison and type juggling rules. When matching a special BSON type the query criteria should use the respective BSON class in the driver (e.g. use MongoDB\BSON\ObjectId to match an ObjectId).

Examples

Return Distinct Values for a Field

The following example identifies the distinct values for the borough field in the restaurants collection in the test database.

<?php

$collection = (new MongoDB\Client)->test->restaurants;

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

var_dump($distinct);

The output would then resemble:

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"
}

Return Distinct Values Using a Filter

The following example identifies the distinct values for the cuisine field in the restaurants collection in the test database for documents where the borough is Queens:

<?php

$collection = (new MongoDB\Client)->test->restaurants;

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

var_dump($distinct);

The output would then resemble:

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"
}

See Also

  • distinct command reference in the MongoDB manual