Skip to content

Commit 26d9632

Browse files
authored
DOCSP-35975: Update many usage example (#2836)
* DOCSP-35975: Update many usage example
1 parent 01dd49f commit 26d9632

File tree

6 files changed

+124
-5
lines changed

6 files changed

+124
-5
lines changed
Lines changed: 48 additions & 0 deletions
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 UpdateManyTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testUpdateMany(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
Movie::insert([
22+
[
23+
'title' => 'Hollywood',
24+
'imdb' => [
25+
'rating' => 9.1,
26+
'votes' => 511,
27+
],
28+
],
29+
[
30+
'title' => 'The Shawshank Redemption',
31+
'imdb' => [
32+
'rating' => 9.3,
33+
'votes' => 1513145,
34+
],
35+
],
36+
]);
37+
38+
// begin-update-many
39+
$updates = Movie::where('imdb.rating', '>', 9.0)
40+
->update(['acclaimed' => true]);
41+
42+
echo 'Updated documents: ' . $updates;
43+
// end-update-many
44+
45+
$this->assertEquals(2, $updates);
46+
$this->expectOutputString('Updated documents: 2');
47+
}
48+
}

docs/usage-examples.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ calls the controller function and returns the result to a web interface.
7272
:maxdepth: 1
7373

7474
/usage-examples/findOne
75+
/usage-examples/insertOne
7576
/usage-examples/insertMany
7677
/usage-examples/updateOne
77-
/usage-examples/insertOne
78+
/usage-examples/updateMany
7879
/usage-examples/deleteOne
7980
/usage-examples/deleteMany
8081
/usage-examples/count

docs/usage-examples/deleteOne.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This usage example performs the following actions:
3232
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
3333
``sample_mflix`` database
3434
- Deletes a document from the ``movies`` collection that matches a query filter
35+
- Prints the number of deleted documents
3536

3637
The example calls the following methods on the ``Movie`` model:
3738

docs/usage-examples/findOne.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ Example
1919
This usage example performs the following actions:
2020

2121
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
22-
``sample_mflix`` database.
23-
- Retrieves a document from the ``movies`` collection that matches a query filter.
22+
``sample_mflix`` database
23+
- Retrieves a document from the ``movies`` collection that matches a query filter
24+
- Prints the retrieved document
2425

2526
The example calls the following methods on the ``Movie`` model:
2627

docs/usage-examples/updateMany.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. _laravel-update-one-usage:
2+
3+
=========================
4+
Update Multiple Documents
5+
=========================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: update many, 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 multiple documents in a collection by calling the ``update()`` method
21+
on a query builder.
22+
23+
Pass a query filter to the ``where()`` method to retrieve documents that meet a
24+
set of criteria. Then, update the matching documents by passing your intended
25+
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 documents from the ``movies`` collection that match a query filter
35+
- Prints the number of updated documents
36+
37+
The example calls the following methods on the ``Movie`` model:
38+
39+
- ``where()``: matches documents in which the value of the ``imdb.rating`` nested field
40+
is greater than ``9``.
41+
- ``update()``: updates the matching documents by adding an ``acclaimed`` field and setting
42+
its value to ``true``. This method returns the number of documents that were successfully
43+
updated.
44+
45+
.. io-code-block::
46+
:copyable: true
47+
48+
.. input:: ../includes/usage-examples/UpdateManyTest.php
49+
:start-after: begin-update-many
50+
:end-before: end-update-many
51+
:language: php
52+
:dedent:
53+
54+
.. output::
55+
:language: console
56+
:visible: false
57+
58+
Updated documents: 20
59+
60+
To learn how to edit your Laravel application to run the usage example, see the
61+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
62+
63+
.. tip::
64+
65+
To learn more about updating data with {+odm-short+}, see the :ref:`laravel-fundamentals-modify-documents`
66+
section of the Write Operations guide.
67+

docs/usage-examples/updateOne.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ Example
3030
This usage example performs the following actions:
3131

3232
- 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.
33+
``sample_mflix`` database
34+
- Updates a document from the ``movies`` collection that matches a query filter
35+
- Prints the number of updated documents
3536

3637
The example calls the following methods on the ``Movie`` model:
3738

0 commit comments

Comments
 (0)