-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41979: Distinct values #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1a32205
4dd9f25
509c146
6241c8a
0ec7b6a
967de4f
2c194ef
e629407
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
| require 'vendor/autoload.php'; | ||
|
|
||
| use MongoDB\BSON\Document; | ||
|
|
||
| $uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
| $client = new MongoDB\Client($uri); | ||
|
|
||
| // start-db-coll | ||
| $collection = $client->sample_restaurants->restaurants; | ||
| // end-db-coll | ||
|
|
||
| // Retrieves distinct values of the "borough" field | ||
| // start-distinct | ||
| $results = $collection->distinct('borough', []); | ||
| foreach ($results as $value) { | ||
| echo json_encode($value) . PHP_EOL; | ||
| } | ||
| // end-distinct | ||
|
|
||
| // Retrieves distinct "borough" field values for documents with a "cuisine" value of "Italian" | ||
| // start-distinct-with-query | ||
| $results = $collection->distinct('borough', ['cuisine' => 'Italian']); | ||
| foreach ($results as $value) { | ||
| echo json_encode($value) . PHP_EOL; | ||
| } | ||
| // end-distinct-with-query | ||
|
|
||
| // Retrieves distinct "name" field values for documents matching the "borough" and "cuisine" fields query | ||
| // and attaches a comment to the operation | ||
| // start-distinct-with-comment | ||
| $query = ['$and' => [['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; | ||
| } | ||
| // end-distinct-with-comment | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,4 +8,5 @@ Read Data from MongoDB | |
| :titlesonly: | ||
| :maxdepth: 1 | ||
|
|
||
| /read/retrieve | ||
| /read/retrieve | ||
| /read/distinct | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,174 @@ | ||||||||||||||
| .. _php-distinct: | ||||||||||||||
|
|
||||||||||||||
| ============================== | ||||||||||||||
| Retrieve Distinct Field Values | ||||||||||||||
| ============================== | ||||||||||||||
|
|
||||||||||||||
| .. contents:: On this page | ||||||||||||||
| :local: | ||||||||||||||
| :backlinks: none | ||||||||||||||
| :depth: 2 | ||||||||||||||
| :class: singlecol | ||||||||||||||
|
|
||||||||||||||
| .. facet:: | ||||||||||||||
| :name: genre | ||||||||||||||
| :values: reference | ||||||||||||||
|
|
||||||||||||||
| .. meta:: | ||||||||||||||
| :keywords: read, unique, code example | ||||||||||||||
|
|
||||||||||||||
| Overview | ||||||||||||||
| -------- | ||||||||||||||
|
|
||||||||||||||
| In this guide, you can learn how to use the {+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 {+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:`Atlas sample datasets </sample-data>`. 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: | ||||||||||||||
|
|
||||||||||||||
| .. literalinclude:: /includes/read/distinct.php | ||||||||||||||
| :language: php | ||||||||||||||
| :dedent: | ||||||||||||||
| :start-after: start-db-coll | ||||||||||||||
| :end-before: end-db-coll | ||||||||||||||
|
|
||||||||||||||
| To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the | ||||||||||||||
| :atlas:`Get Started with Atlas </getting-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: | ||||||||||||||
|
|
||||||||||||||
| .. io-code-block:: | ||||||||||||||
| :copyable: | ||||||||||||||
|
|
||||||||||||||
| .. input:: /includes/read/distinct.php | ||||||||||||||
| :start-after: start-distinct | ||||||||||||||
| :end-before: end-distinct | ||||||||||||||
| :language: php | ||||||||||||||
| :dedent: | ||||||||||||||
|
|
||||||||||||||
| .. output:: | ||||||||||||||
| :visible: false | ||||||||||||||
|
||||||||||||||
| .. output:: | |
| :visible: false | |
| .. output:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: link broken until #103 is merged
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: Make this a sentence since there is only one link here
| To learn more about the ``MongoDB\Collection::distinct()`` method, see the following | |
| API documentation: | |
| - `distinct() <{+api+}/method/MongoDBCollection-distinct/>`__ | |
| To learn more about the ``MongoDB\Collection::distinct()`` method, see the | |
| `distinct() API documentation <{+api+}/method/MongoDBCollection-distinct/>`__. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
$andoperator strictly necessary here? Wouldnt the following achieve the same while being easier to read?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!