-
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
Merged
norareidy
merged 19 commits into
mongodb:4.1
from
norareidy:DOCSP-35977-find-multiple-usage
Apr 22, 2024
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2c5de43
DOCSP-35977: Find multiple usage example
norareidy 6f69e4b
apply phpcbf formatting
norareidy 2e5c74d
fix format
norareidy 0188f34
Merge branch 'DOCSP-35977-find-multiple-usage' of github.com:norareid…
norareidy 79c4e9f
id vals
norareidy 24dff63
facets, keywords
norareidy 0747bae
CC feedback
norareidy bbf696e
apply phpcbf formatting
norareidy a9dd4c5
cursor
norareidy 84351df
Merge branch 'DOCSP-35977-find-multiple-usage' of github.com:norareid…
norareidy 87aa601
test
norareidy fa8f4cc
removing
norareidy c08ef51
typo
norareidy 60645db
Update docs/includes/usage-examples/FindManyTest.php
ff66aca
Merge branch '4.1' into DOCSP-35977-find-multiple-usage
f81a15b
PRR fixes
a4cd130
PRR fixes
64bc331
Merge branch '4.1' into DOCSP-35977-find-multiple-usage
193504d
Makes the test stricter with data outside the filter
GromNaN 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,44 @@ | ||
<?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, | ||
], | ||
[ | ||
'title' => 'Basketball', | ||
'runtime' => 600, | ||
], | ||
]); | ||
|
||
// begin-find | ||
$movies = Movie::where('runtime', '>', 900) | ||
->orderBy('_id') | ||
->get(); | ||
// end-find | ||
|
||
$this->assertEquals(2, $movies->count()); | ||
} | ||
} |
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,85 @@ | ||
.. _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 creating a query | ||
builder using a method such as ``Model::where()`` or by using the ``DB`` | ||
facade, and then chaining the ``get()`` method to retrieve the result. | ||
|
||
Pass a query filter to the ``where()`` method to retrieve documents that meet a | ||
set of criteria. When you call the ``get()`` method, 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. | ||
|
||
To learn more about query builder methods, see the :ref:`laravel-query-builder` | ||
guide. | ||
|
||
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 | ||
- ``get()``: retrieves the query results as a Laravel collection object | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: ../includes/usage-examples/FindManyTest.php | ||
:start-after: begin-find | ||
:end-before: end-find | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: json | ||
:visible: false | ||
|
||
// Results are truncated | ||
|
||
[ | ||
{ | ||
"_id": ..., | ||
"runtime": 1256, | ||
"title": "Centennial", | ||
..., | ||
}, | ||
{ | ||
"_id": ..., | ||
"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. |
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.
I've added an item with runtime <= 900 for the test to be complete.