Skip to content

DOCSP-35977: Find multiple usage example #2833

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 19 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
35 changes: 35 additions & 0 deletions docs/includes/usage-examples/FindManyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Movie;
use MongoDB\Laravel\Tests\TestCase;

class FindManyTest extends TestCase
{
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testFindMany(): void
{
require_once __DIR__ . '/Movie.php';

Movie::truncate();

// begin-find
$movies = Movie::where('runtime', '>', 800)
->orderBy('_id')
->get();

foreach ($movies as $m) {
echo $m->toJson() . '<br>';
}

// end-find

$this->assertInstanceOf(Movie::class, $movie);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
I think $movie hasn't been declared, so I would omit this assertion, or place it in a loop similar to lines 27-29.

}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ calls the controller function and returns the result to a web interface.
:maxdepth: 1

/usage-examples/findOne
/usage-examples/find
/usage-examples/updateOne
107 changes: 107 additions & 0 deletions docs/usage-examples/find.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
.. _laravel-find-usage:

=======================
Find Multiple Documents
=======================

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: find many, retrieve, code example

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

You can retrieve multiple documents from a collection by calling the ``where()`` method
on an Eloquent model or a query builder.

Pass a query filter to the ``where()`` method to retrieve documents that meet a
set of criteria. MongoDB returns the matching documents according to their
:term:`natural order` in the database or according to the sort order that you can specify
by using the ``orderBy()`` method.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question:
Can you access the cursor by chaining the "cursor()" method like this? If that's the preferred method of retrieving multiple results, I think we should show that.


Example
-------

This usage example performs the following actions:

- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
``sample_mflix`` database
- Retrieves documents from the ``movies`` collection that match a query filter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
The example shows code to print out the retrieved documents, so I think it should either be mentioned or the code should be omitted.

The example calls the following methods on the ``Movie`` model:

- ``where()``: matches documents in which the value of the ``runtime`` field is greater than ``800``
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values
- ``get()``: retrieves the query results

.. io-code-block::
:copyable: true

.. input:: ../includes/usage-examples/FindManyTest.php
:start-after: begin-find
:end-before: end-find
:language: php
:dedent:

.. output::
:language: console
:visible: false

// Results are truncated

{
"_id": "...",
"plot": "The economic and cultural growth of Colorado spanning two centuries from the mid-1700s to the late-1970s.",
"genres": [
"Action",
"Adventure",
"Drama"
],
"runtime": 1256,
...
"title": "Centennial",
...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
I think showing the expanded "title", "runtime", and "_id" fields could make the output easier to read and would be sufficient to help the reader understand the query filter and sort order. I think the rest could be hidden with "..."

Perhaps you'd want matches that also aren't easy to mistake for
another more popular title such as "Taken" since the "runtime" field could be a point of confusion.

Another solution could be to avoid using the "_id" sort and instead using the "runtime" value so that you could omit the "_id" field.

}

{
"_id": "...",
"plot": "A documentary on the history of the sport with major topics including Afro-American players, player/team
owner relations and the resilience of the game.",
"genres": [
"Documentary",
"History",
"Sport"
],
"runtime": 1140,
...
"title": "Baseball",
...
}

{
"_id": "...",
"plot": "Taken spans five decades and four generations, centering on three families: the Keys, Crawfords, and Clarkes.
World War II veteran Russell Keys is plagued by nightmares of his abduction by ...",
"genres": [
"Drama",
"Sci-Fi"
],
"runtime": 877,
...
"title": "Taken",
...
}

For instructions on editing your Laravel application to run the usage example, see the
:ref:`Usage Example landing page <laravel-usage-examples>`.

.. tip::

To learn more about retrieving documents with {+odm-short+}, see the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "where()" is one of many methods you can chain to the model class. Maybe this sentence could be modified to indicate there are other ways of retrieving documents.

"To learn about other ways to retrieve documents..."

:ref:`laravel-fundamentals-retrieve` guide
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:ref:`laravel-fundamentals-retrieve` guide
:ref:`laravel-fundamentals-retrieve` guide.