Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f7bf77

Browse files
Merge 4.1 into 4.2 (#2899)
2 parents 10b48dd + 6ac2b6a commit 8f7bf77

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 FindManyTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testFindMany(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
Movie::insert([
22+
[
23+
'title' => 'Centennial',
24+
'runtime' => 1256,
25+
],
26+
[
27+
'title' => 'Baseball',
28+
'runtime' => 1140,
29+
],
30+
[
31+
'title' => 'Basketball',
32+
'runtime' => 600,
33+
],
34+
]);
35+
36+
// begin-find
37+
$movies = Movie::where('runtime', '>', 900)
38+
->orderBy('_id')
39+
->get();
40+
// end-find
41+
42+
$this->assertEquals(2, $movies->count());
43+
}
44+
}

‎docs/usage-examples.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ calls the controller function and returns the result to a web interface.
7272
:maxdepth: 1
7373

7474
/usage-examples/findOne
75+
/usage-examples/find
7576
/usage-examples/insertOne
7677
/usage-examples/insertMany
7778
/usage-examples/updateOne

‎docs/usage-examples/find.txt‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
.. _laravel-find-usage:
2+
3+
=======================
4+
Find Multiple Documents
5+
=======================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: find many, retrieve, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
19+
20+
You can retrieve multiple documents from a collection by creating a query
21+
builder using a method such as ``Model::where()`` or by using the ``DB``
22+
facade, and then chaining the ``get()`` method to retrieve the result.
23+
24+
Pass a query filter to the ``where()`` method to retrieve documents that meet a
25+
set of criteria. When you call the ``get()`` method, MongoDB returns the
26+
matching documents according to their :term:`natural order` in the database or
27+
according to the sort order that you can specify by using the ``orderBy()``
28+
method.
29+
30+
To learn more about query builder methods, see the :ref:`laravel-query-builder`
31+
guide.
32+
33+
Example
34+
-------
35+
36+
This usage example performs the following actions:
37+
38+
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
39+
``sample_mflix`` database
40+
- Retrieves and prints documents from the ``movies`` collection that match a query filter
41+
42+
The example calls the following methods on the ``Movie`` model:
43+
44+
- ``where()``: matches documents in which the value of the ``runtime`` field is greater than ``900``
45+
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values
46+
- ``get()``: retrieves the query results as a Laravel collection object
47+
48+
.. io-code-block::
49+
:copyable: true
50+
51+
.. input:: ../includes/usage-examples/FindManyTest.php
52+
:start-after: begin-find
53+
:end-before: end-find
54+
:language: php
55+
:dedent:
56+
57+
.. output::
58+
:language: json
59+
:visible: false
60+
61+
// Results are truncated
62+
63+
[
64+
{
65+
"_id": ...,
66+
"runtime": 1256,
67+
"title": "Centennial",
68+
...,
69+
},
70+
{
71+
"_id": ...,
72+
"runtime": 1140,
73+
"title": "Baseball",
74+
...,
75+
},
76+
...
77+
]
78+
79+
For instructions on editing your Laravel application to run the usage example, see the
80+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
81+
82+
.. tip::
83+
84+
To learn about other ways to retrieve documents with {+odm-short+}, see the
85+
:ref:`laravel-fundamentals-retrieve` guide.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /