Overview
In this guide, you can learn how to use the MongoDB PHP Library to retrieve the distinct values of a specified field across a collection.
Within a collection, different documents might contain different values for a single field. For example, one document in a restaurants collection has a borough value of 'Manhattan', and another has a borough value of 'Queens'. By using the MongoDB PHP Library, you can retrieve all the unique values that a field contains across multiple documents in a collection.
Sample Data
The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To access this collection from your PHP application, instantiate a MongoDB\Client that connects to an Atlas cluster and assign the following value to your $collection variable:
$collection = $client->sample_restaurants->restaurants;
To learn how to create a free MongoDB deployment and load the sample datasets, see the MongoDB Get Started guide.
MongoDB\Collection::distinct() Method
To retrieve the distinct values for a specified field, call the MongoDB\Collection::distinct() method and pass in the name of the field you want to find distinct values for.
Retrieve Distinct Values Across a Collection
The following example retrieves the distinct values of the borough field in the restaurants collection:
$results = $collection->distinct('borough', []); foreach ($results as $value) { echo json_encode($value), PHP_EOL; }
"Bronx" "Manhattan" "Missing" "Queens" "Staten Island"
The operation returns an array that stores each distinct borough field value. Although several documents have the same value in the borough field, each value appears in the results only once.
Retrieve Distinct Values Across Specified Documents
You can provide a query filter to the distinct() method to find the distinct field values across a subset of documents in a collection. A query filter is an expression that specifies search criteria used to match documents in an operation. For more information about creating a query filter, see the Specify a Query guide.
The following example retrieves the distinct values of the borough field for all documents that have a cuisine field value of 'Italian':
$results = $collection->distinct('borough', ['cuisine' => 'Italian']); foreach ($results as $value) { echo json_encode($value), PHP_EOL; }
"Bronx" "Manhattan" "Queens" "Staten Island"
Modify Distinct Behavior
You can modify the behavior of the distinct() method by passing an array that specifies option values. The following table describes some options you can set to customize the operation:
Option | Description |
|---|---|
| The collation to use for the operation. To learn more, see the
Collation section of this page. |
| The maximum amount of time in milliseconds that the operation can run. |
| The comment to attach to the operation. |
| The read preference to use for the operation. To learn more, see
Read Preference in the Server manual. |
| The index to use for the operation. |
The following example retrieves the distinct values of the name field for all documents that have a borough field value of 'Bronx' and a cuisine field value of 'Pizza'. It also specifies the comment field in an options array to add a comment to the operation:
$query = ['borough' => 'Bronx', 'cuisine' => 'Pizza']; $options = ['comment' => 'Bronx pizza restaurants']; $results = $collection->distinct('name', $query, $options); foreach ($results as $value) { echo json_encode($value), PHP_EOL; }
"$1.25 Pizza" "18 East Gunhill Pizza" "2 Bros" "Aenos Pizza" "Alitalia Pizza Restaurant" "Amici Pizza And Pasta" "Angie'S Cafe Pizza" ...
Collation
To specify a collation for your operation, pass an $options array parameter that sets the collation option to the operation method. Assign the collation option to an array that configures the collation rules.
The following table describes the fields you can set to configure the collation:
Field | Description |
|---|---|
| (Required) Specifies the International Components for Unicode (ICU) locale. For a
list of supported locales, see Collation Locales and Default Parameters
in the MongoDB Server manual. |
| (Optional) Specifies whether to include case comparison. |
| (Optional) Specifies the sort order of case differences during tertiary
level comparisons. |
| (Optional) Specifies the level of comparison to perform, as defined in the
ICU documentation. |
| (Optional) Specifies whether the driver compares numeric strings as numbers. |
| (Optional) Specifies whether the library considers whitespace and punctuation as base
characters for comparison purposes. |
| (Optional) Specifies which characters the library considers ignorable when
the |
| (Optional) Specifies whether strings containing diacritics sort from the back of the string
to the front. |
To learn more about collation and the possible values for each field, see the Collation entry in the MongoDB Server manual.
API Documentation
To learn more about the distinct() method, see MongoDB\Collection::distinct() in the API documentation.