-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 13 commits
2c5de43
6f69e4b
2e5c74d
0188f34
79c4e9f
24dff63
0747bae
bbf696e
a9dd4c5
84351df
87aa601
fa8f4cc
c08ef51
60645db
ff66aca
f81a15b
a4cd130
64bc331
193504d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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(); | ||
Movie::insert([ | ||
[ | ||
'title' => 'Centennial', | ||
'runtime' => 1256, | ||
], | ||
[ | ||
'title' => 'Baseball', | ||
'runtime' => 1140, | ||
], | ||
]); | ||
|
||
// begin-find | ||
foreach ( | ||
Movie::where('runtime', '>', 900) | ||
->orderBy('_id') | ||
->cursor() as $movie | ||
) { | ||
echo $movie->toJson() . '<br>'; | ||
} | ||
|
||
// end-find | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
.. _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. | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. | ||
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. Question: |
||
|
||
Example | ||
------- | ||
|
||
This usage example performs the following actions: | ||
|
||
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the | ||
``sample_mflix`` database | ||
- Retrieves and prints documents from the ``movies`` collection that match a query filter | ||
|
||
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. Suggestion: |
||
The example calls the following methods on the ``Movie`` model: | ||
|
||
- ``where()``: matches documents in which the value of the ``runtime`` field is greater than ``900`` | ||
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values | ||
- ``cursor()``: retrieves the query results by loading each ``Movie`` model into memory one at a time | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. 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": "573a1397f29313caabce69db", | ||
... | ||
"runtime": 1256, | ||
... | ||
"title": "Centennial", | ||
... | ||
} | ||
|
||
{ | ||
"_id": "573a1399f29313caabcee1aa", | ||
... | ||
"runtime": 1140, | ||
... | ||
"title": "Baseball", | ||
... | ||
} | ||
|
||
For instructions on editing your Laravel application to run the usage example, see the | ||
:ref:`Usage Examples landing page <laravel-usage-examples>`. | ||
|
||
.. tip:: | ||
|
||
To learn about other ways to retrieve documents with {+odm-short+}, see the | ||
:ref:`laravel-fundamentals-retrieve` guide. |
Uh oh!
There was an error while loading. Please reload this page.