-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35972: Distinct values usage example #2846
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
Merged
ccho-mongodb
merged 7 commits into
mongodb:4.1
from
norareidy:DOCSP-35972-distinct-usage
Apr 16, 2024
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34c5b70
DOCSP-35972: Distinct values usage example
norareidy 47ee39f
quotes
norareidy ab8d08f
RR feedback
norareidy 97cb69d
directors field
norareidy c2a0f08
typo
norareidy aaa03c2
Merge branch '4.1' into DOCSP-35972-distinct-usage
b7e5c55
Merge branch '4.1' into DOCSP-35972-distinct-usage
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class DistinctTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testDistinct(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
Movie::truncate(); | ||
Movie::insert([ | ||
[ | ||
'title' => 'The Big House', | ||
'imdb' => [ | ||
'rating' => 7.3, | ||
'votes' => 1161, | ||
], | ||
], | ||
[ | ||
'title' => 'The Divorcee', | ||
'imdb' => [ | ||
'rating' => 6.9, | ||
'votes' => 1740, | ||
], | ||
], | ||
[ | ||
'title' => 'Morocco', | ||
'imdb' => [ | ||
'rating' => 7.3, | ||
'votes' => 3566, | ||
], | ||
], | ||
]); | ||
|
||
// begin-distinct | ||
$ratings = Movie::where('year', 1930) | ||
->select('imdb.rating') | ||
->distinct() | ||
->get(); | ||
|
||
echo $ratings; | ||
// end-distinct | ||
|
||
$this->expectOutputString('[[6.9],[7.3]]'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
.. _laravel-distinct-usage: | ||
|
||
============================== | ||
Retrieve Distinct Field Values | ||
============================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: unique, different, code example | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 1 | ||
:class: singlecol | ||
|
||
You can retrieve distinct field values of documents in a collection by calling the ``distinct()`` | ||
method on an object collection or a query builder. | ||
|
||
To retrieve distinct field values, pass a query filter to the ``where()`` method and a field name | ||
to the ``select()`` method. Then, call ``distinct()`` to return the unique field values of documents | ||
that match the query filter. | ||
|
||
Example | ||
------- | ||
|
||
This usage example performs the following actions: | ||
|
||
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the | ||
``sample_mflix`` database | ||
- Retrieves distinct field values of documents from the ``movies`` collection that match a query filter | ||
- Prints the distinct values | ||
|
||
The example calls the following methods on the ``Movie`` model: | ||
|
||
- ``where()``: matches documents in which the value of the ``year`` field is ``1930``. | ||
- ``select()``: retrieves the matching documents' ``imdb.rating`` field values. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: is there a better field to select for than a numerical field? Like genre or director maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, changed to the directors field |
||
- ``distinct()``: accesses the unique values of the ``imdb.rating`` field for each matching | ||
document. This method returns a list of values. | ||
- ``get()``: retrieves the query results. | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: ../includes/usage-examples/DistinctTest.php | ||
:start-after: begin-distinct | ||
:end-before: end-distinct | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
[[5.8],[6],[6.7],[6.9],[7.2],[7.3],[7.5]] | ||
|
||
To learn how to edit your Laravel application to run the usage example, see the | ||
:ref:`Usage Examples landing page <laravel-usage-examples>`. | ||
|
||
.. tip:: | ||
|
||
For more information about query filters, see the :ref:`laravel-retrieve-matching` section of | ||
the Read Operations guide. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.