Skip to content

Merge 4.1 into 4.2 #2869

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 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/includes/usage-examples/CountTest.php
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
$count = Movie::where('genres', 'Biography')
->count();

echo 'Number of documents: ' . $count;
// end-count

$this->assertEquals(2, $count);
$this->expectOutputString('Number of documents: 2');
}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ calls the controller function and returns the result to a web interface.
/usage-examples/updateOne
/usage-examples/deleteOne
/usage-examples/deleteMany
/usage-examples/count
57 changes: 57 additions & 0 deletions docs/usage-examples/count.txt
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 a collection of models or a query builder.

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.

.. 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>`.