-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35970: Find one usage example #2768
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 20 commits
ffd9d2c
e92a891
bf46030
6c12f4d
e2145b6
e9aa9de
4c4684d
3bcad19
1dc60a6
ab116ce
f6cc811
871f721
70c7014
c6c6e8f
c68a476
7b2f1dc
ac545b6
6b1f418
b3c921e
35e1dcc
f5f4a44
c11f0cd
5f58f70
d775d8b
5bf1b71
fc6f15b
bf98e98
420e60a
901a4e4
a8a6558
b75a786
4d13dc6
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
use function print_r; | ||
|
||
class UpdateOneTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function updateOne(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
// begin-find-one | ||
$movie = Movie::where('directors', 'Rob Reiner') | ||
->orderBy('_id') | ||
->first(); | ||
|
||
print_r($movie->toJson()); | ||
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. Do we really want to display the results with 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. How would you prefer to display the results? Originally I used a view, but I changed it to 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. Since it's a simple string, I modified to use |
||
// end-find-one | ||
|
||
// <optionally, add assertions> | ||
} | ||
|
||
// ... <add additional test cases here> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use MongoDB\Laravel\Eloquent\Model; | ||
|
||
class Movie extends Model | ||
{ | ||
protected $connection = 'mongodb'; | ||
protected $collection = 'movies'; | ||
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot']; | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
.. _laravel-find-one-usage: | ||
|
||
=============== | ||
Find a Document | ||
=============== | ||
|
||
You can retrieve a single document from a collection by calling the ``where()`` and | ||
``first()`` methods on an Eloquent model or a query builder. | ||
norareidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Pass a query filter to the ``where()`` method and then call the ``first()`` method to | ||
return one document in the collection that matches the filter. If multiple documents match | ||
the query filter, ``first()`` returns the first matching document according to the documents' | ||
:term:`natural order` in the database or according to the sort order that you can specify | ||
by using the ``orderBy()`` method. | ||
|
||
Example | ||
------- | ||
|
||
This usage example performs the following actions: | ||
|
||
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the | ||
``sample_mflix`` database. | ||
- Retrieves a document from the ``movies`` collection that matches a query filter. | ||
|
||
The example calls the following methods on the ``Movie`` model: | ||
|
||
- ``where()``: matches documents in which the value of the ``directors`` field includes ``'Rob Reiner'``. | ||
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values. | ||
- ``first()``: retrieves only the first matching document. | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: ../includes/usage-examples/FindOneTest.php | ||
:start-after: begin-find-one | ||
:end-before: end-find-one | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
// Result is truncated | ||
|
||
{ | ||
"_id": "573a1398f29313caabce94a3", | ||
"plot": "Spinal Tap, one of England's loudest bands, is chronicled by film director | ||
Marty DeBergi on what proves to be a fateful tour.", | ||
"genres": [ | ||
"Comedy", | ||
"Music" | ||
], | ||
"runtime": 82, | ||
"metacritic": 85, | ||
"rated": "R", | ||
"cast": [ | ||
"Rob Reiner", | ||
"Kimberly Stringer", | ||
"Chazz Dominguez", | ||
"Shari Hall" | ||
], | ||
"poster": "https://m.media-amazon.com/images/M/MV5BMTQ2MTIzMzg5Nl5BMl5BanBnXkFtZTgwOTc5NDI1MDE@._V1_SY1000_SX677_AL_.jpg", | ||
"title": "This Is Spinal Tap", | ||
... | ||
} | ||
|
||
|
||
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 | ||
:ref:`laravel-fundamentals-retrieve` guide |
Uh oh!
There was an error while loading. Please reload this page.