File tree 3 files changed +100
-0
lines changed
3 files changed +100
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace App \Http \Controllers ;
6
+
7
+ use App \Models \Movie ;
8
+ use MongoDB \Laravel \Tests \TestCase ;
9
+
10
+ class CountTest extends TestCase
11
+ {
12
+ /**
13
+ * @runInSeparateProcess
14
+ * @preserveGlobalState disabled
15
+ */
16
+ public function testCount (): void
17
+ {
18
+ require_once __DIR__ . '/Movie.php ' ;
19
+
20
+ Movie::truncate ();
21
+ Movie::insert ([
22
+ [
23
+ 'title ' => 'Young Mr. Lincoln ' ,
24
+ 'genres ' => ['Biography ' , 'Drama ' ],
25
+ ],
26
+ [
27
+ 'title ' => 'Million Dollar Mermaid ' ,
28
+ 'genres ' => ['Biography ' , 'Drama ' , 'Musical ' ],
29
+ ],
30
+ ]);
31
+
32
+ // begin-count
33
+ $ count = Movie::where ('genres ' , 'Biography ' )
34
+ ->count ();
35
+
36
+ echo 'Number of documents: ' . $ count ;
37
+ // end-count
38
+
39
+ $ this ->assertEquals (2 , $ count );
40
+ $ this ->expectOutputString ('Number of documents: 2 ' );
41
+ }
42
+ }
Original file line number Diff line number Diff line change @@ -75,3 +75,4 @@ calls the controller function and returns the result to a web interface.
75
75
/usage-examples/updateOne
76
76
/usage-examples/deleteOne
77
77
/usage-examples/deleteMany
78
+ /usage-examples/count
Original file line number Diff line number Diff line change
1
+ .. _laravel-count-usage:
2
+
3
+ ===============
4
+ Count Documents
5
+ ===============
6
+
7
+ .. facet::
8
+ :name: genre
9
+ :values: reference
10
+
11
+ .. meta::
12
+ :keywords: total, code example
13
+
14
+ .. contents:: On this page
15
+ :local:
16
+ :backlinks: none
17
+ :depth: 1
18
+ :class: singlecol
19
+
20
+ You can count the number of documents returned by a query by calling the ``where()`` and
21
+ ``count()`` methods on a collection of models or a query builder.
22
+
23
+ To return the number of documents that match a filter, pass the query filter to the ``where()``
24
+ method and call the ``count()`` method.
25
+
26
+ Example
27
+ -------
28
+
29
+ This usage example performs the following actions:
30
+
31
+ - Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
32
+ ``sample_mflix`` database
33
+ - Counts the documents from the ``movies`` collection that match a query filter
34
+ - Prints the matching document count
35
+
36
+ The example calls the following methods on the ``Movie`` model:
37
+
38
+ - ``where()``: Matches documents in which the value of the ``genres`` field includes ``"Biography"``.
39
+ - ``count()``: Counts the number of matching documents. This method returns an integer value.
40
+
41
+ .. io-code-block::
42
+
43
+ .. input:: ../includes/usage-examples/CountTest.php
44
+ :start-after: begin-count
45
+ :end-before: end-count
46
+ :language: php
47
+ :dedent:
48
+
49
+ .. output::
50
+ :language: console
51
+ :visible: false
52
+
53
+ Matching documents: 1267
54
+
55
+
56
+ To learn how to edit your Laravel application to run the usage example, see the
57
+ :ref:`Usage Examples landing page <laravel-usage-examples>`.
You can’t perform that action at this time.
0 commit comments