-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35982: Count usage example #2850
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
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
125041f
DOCSP-35982: Count usage example
norareidy ceddbda
apply phpcbf formatting
norareidy 4707b1a
MW feedback
norareidy 7f64cf0
Merge branch 'DOCSP-35982-count-usage' of github.com:norareidy/larave…
norareidy 46746ad
JT feedback
norareidy aa4beae
Merge branch '4.1' into DOCSP-35982-count-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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class CountTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testCount(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
Movie::truncate(); | ||
Movie::insert([ | ||
[ | ||
'title' => 'Young Mr. Lincoln', | ||
'genres' => ['Biography', 'Drama'], | ||
], | ||
[ | ||
'title' => 'Million Dollar Mermaid', | ||
'genres' => ['Biography', 'Drama', 'Musical'], | ||
], | ||
]); | ||
|
||
// begin-count | ||
$biographies = Movie::where('genres', 'Biography') | ||
->count(); | ||
|
||
echo 'Number of documents: ' . $biographies; | ||
// end-count | ||
|
||
$this->assertEquals(2, $biographies); | ||
$this->expectOutputString('Number of documents: 2'); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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,57 @@ | ||||||||||
.. _laravel-count-usage: | ||||||||||
|
||||||||||
=============== | ||||||||||
Count Documents | ||||||||||
=============== | ||||||||||
|
||||||||||
.. facet:: | ||||||||||
:name: genre | ||||||||||
:values: reference | ||||||||||
|
||||||||||
.. meta:: | ||||||||||
:keywords: total, code example | ||||||||||
|
||||||||||
.. contents:: On this page | ||||||||||
:local: | ||||||||||
:backlinks: none | ||||||||||
:depth: 1 | ||||||||||
:class: singlecol | ||||||||||
|
||||||||||
You can count the number of documents returned by a query by calling the ``where()`` and | ||||||||||
``count()`` methods on an object collection or a query builder. | ||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
To return the number of documents that match a filter, pass the query filter to the ``where()`` | ||||||||||
method and call the ``count()`` method. | ||||||||||
|
||||||||||
Example | ||||||||||
------- | ||||||||||
|
||||||||||
This usage example performs the following actions: | ||||||||||
|
||||||||||
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the | ||||||||||
``sample_mflix`` database | ||||||||||
- Counts the documents from the ``movies`` collection that match a query filter | ||||||||||
- Prints the matching document count | ||||||||||
|
||||||||||
The example calls the following methods on the ``Movie`` model: | ||||||||||
|
||||||||||
- ``where()``: matches documents in which the value of the ``genres`` field includes ``"Biography"``. | ||||||||||
- ``count()``: counts the number of matching documents. This method returns an integer value. | ||||||||||
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.
Suggested change
|
||||||||||
|
||||||||||
.. io-code-block:: | ||||||||||
|
||||||||||
.. input:: ../includes/usage-examples/CountTest.php | ||||||||||
:start-after: begin-count | ||||||||||
:end-before: end-count | ||||||||||
:language: php | ||||||||||
:dedent: | ||||||||||
|
||||||||||
.. output:: | ||||||||||
:language: console | ||||||||||
:visible: false | ||||||||||
|
||||||||||
Matching documents: 1267 | ||||||||||
|
||||||||||
|
||||||||||
To learn how to edit your Laravel application to run the usage example, see the | ||||||||||
:ref:`Usage Examples landing page <laravel-usage-examples>`. |
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.
The 2nd example for "a collection of object" would be:
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.
Does the
where()
method return a collection of objects? If so, it seems (from my understanding) accurate to say that I'm callingcount()
on a "collection of objects" if I chaincount()
right afterwhere()
. If not, I'll change the other code examples that mention a "collection of objects" as well to useget()
.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.
where()
returns a query builder. Calling->get()
runs the find query and returns the collection of model instances.