Skip to content

Commit 8eb9271

Browse files
norareidyGromNaN
andauthoredApr 3, 2024
DOCSP-35974: Update one usage example (mongodb#2781)
* DOCSP-35974: Update one usage example * fixes * other usage ex changes * reworking the example * add tip, spacing * restructuring * reword tip * fixes * edits * reword * add more running info * small change * edits * source constant * test file * move test file * single quotes * print syntax * print statement again * JT feedback * apply phpcbf formatting * code * apply phpcbf formatting * JT feedback * Fix UpdateOneTest example * add to TOC --------- Co-authored-by: norareidy <[email protected]> Co-authored-by: Jérôme Tamarelle <[email protected]>
1 parent 9a616f9 commit 8eb9271

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed
 
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
7+
class Movie extends Model
8+
{
9+
protected $connection = 'mongodb';
10+
protected $collection = 'movies';
11+
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 UpdateOneTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testUpdateOne(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
Movie::insert([
22+
[
23+
'title' => 'Carol',
24+
'imdb' => [
25+
'rating' => 7.2,
26+
'votes' => 125000,
27+
],
28+
],
29+
]);
30+
31+
// begin-update-one
32+
$updates = Movie::where('title', 'Carol')
33+
->orderBy('_id')
34+
->first()
35+
->update([
36+
'imdb' => [
37+
'rating' => 7.3,
38+
'votes' => 142000,
39+
],
40+
]);
41+
42+
echo 'Updated documents: ' . $updates;
43+
// end-update-one
44+
45+
$this->assertTrue($updates);
46+
$this->expectOutputString('Updated documents: 1');
47+
}
48+
}

‎docs/usage-examples.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ a result. To run the operation, you can copy the sample code to a controller end
6565
Laravel application.
6666

6767
To view the expected output of the operation, you can add a web route to your application that
68-
calls the controller function and returns the result to a web interface.
68+
calls the controller function and returns the result to a web interface.
69+
70+
.. toctree::
71+
:titlesonly:
72+
:maxdepth: 1
73+
74+
/usage-examples/updateOne

‎docs/usage-examples/updateOne.txt

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. _laravel-update-one-usage:
2+
3+
=================
4+
Update a Document
5+
=================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: update one, modify, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
19+
20+
You can update a document in a collection by retrieving a single document and calling
21+
the ``update()`` method on an Eloquent model or a query builder.
22+
23+
Pass a query filter to the ``where()`` method, sort the matching documents, and call the
24+
``first()`` method to retrieve only the first document. Then, update this matching document
25+
by passing your intended document changes to the ``update()`` method.
26+
27+
Example
28+
-------
29+
30+
This usage example performs the following actions:
31+
32+
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
33+
``sample_mflix`` database.
34+
- Updates a document from the ``movies`` collection that matches a query filter.
35+
36+
The example calls the following methods on the ``Movie`` model:
37+
38+
- ``where()``: matches documents in which the value of the ``title`` field is ``'Carol'``.
39+
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values.
40+
- ``first()``: retrieves only the first matching document.
41+
- ``update()``: updates the value of the ``imdb.rating`` nested field to from ``6.9`` to
42+
``7.3``. This method also updates the ``imdb.votes`` nested field from ``493`` to ``142000``.
43+
44+
.. io-code-block::
45+
:copyable: true
46+
47+
.. input:: ../includes/usage-examples/UpdateOneTest.php
48+
:start-after: begin-update-one
49+
:end-before: end-update-one
50+
:language: php
51+
:dedent:
52+
53+
.. output::
54+
:language: console
55+
:visible: false
56+
57+
Updated documents: 1
58+
59+
For instructions on editing your Laravel application to run the usage example, see the
60+
:ref:`Usage Example landing page <laravel-usage-examples>`.
61+
62+
.. tip::
63+
64+
To learn more about updating data with {+odm-short+}, see the `Updates
65+
<https://laravel.com/docs/{+laravel-docs-version+}/eloquent#updates>`__ section of the
66+
Laravel documentation.
67+

0 commit comments

Comments
 (0)
Please sign in to comment.