-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35975: Update many usage example #2836
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
ccho-mongodb
merged 6 commits into
mongodb:4.1
from
norareidy:DOCSP-35975-update-multiple-usage
Apr 19, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cef2635
DOCSP-35975: Update many usage example
norareidy c7f0055
editing code, wording
norareidy 9bacacb
merge
norareidy f03ca2d
CC feedback
norareidy d6d70d9
JT feedback
norareidy bc6082b
Merge branch '4.1' into DOCSP-35975-update-multiple-usage
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class UpdateManyTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testUpdateMany(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
Movie::truncate(); | ||
Movie::insert([ | ||
[ | ||
'title' => 'Hollywood', | ||
'imdb' => [ | ||
'rating' => 9.1, | ||
'votes' => 511, | ||
], | ||
], | ||
[ | ||
'title' => 'The Shawshank Redemption', | ||
'imdb' => [ | ||
'rating' => 9.3, | ||
'votes' => 1513145, | ||
], | ||
], | ||
]); | ||
|
||
// begin-update-many | ||
$updates = Movie::where('imdb.rating', '>', 9.0) | ||
->update(['acclaimed' => true]); | ||
|
||
echo 'Updated documents: ' . $updates; | ||
// end-update-many | ||
|
||
$this->assertEquals(2, $updates); | ||
$this->expectOutputString('Updated documents: 2'); | ||
} | ||
} |
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
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,67 @@ | ||
.. _laravel-update-one-usage: | ||
|
||
========================= | ||
Update Multiple Documents | ||
========================= | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: update many, modify, code example | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 1 | ||
:class: singlecol | ||
|
||
You can update multiple documents in a collection by calling the ``update()`` method | ||
on a query builder. | ||
|
||
Pass a query filter to the ``where()`` method to retrieve documents that meet a | ||
set of criteria. Then, update the matching documents by passing your intended | ||
document changes to the ``update()`` method. | ||
|
||
Example | ||
------- | ||
|
||
This usage example performs the following actions: | ||
|
||
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the | ||
``sample_mflix`` database | ||
- Updates documents from the ``movies`` collection that match a query filter | ||
- Prints the number of updated documents | ||
|
||
The example calls the following methods on the ``Movie`` model: | ||
|
||
- ``where()``: matches documents in which the value of the ``imdb.rating`` nested field | ||
is greater than ``9``. | ||
- ``update()``: updates the matching documents by adding an ``acclaimed`` field and setting | ||
its value to ``true``. This method returns the number of documents that were successfully | ||
updated. | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: ../includes/usage-examples/UpdateManyTest.php | ||
:start-after: begin-update-many | ||
:end-before: end-update-many | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
Updated documents: 20 | ||
|
||
To learn how to edit your Laravel application to run the usage example, see the | ||
:ref:`Usage Examples landing page <laravel-usage-examples>`. | ||
|
||
.. tip:: | ||
|
||
To learn more about updating data with {+odm-short+}, see the :ref:`laravel-fundamentals-modify-documents` | ||
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. Note: since this links to a section of the Write operations page, this PR will be merged after #2808 |
||
section of the Write Operations guide. | ||
|
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
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.
Similar to prior feedback, I think printing the output should be mentioned or the code that prints should be hidden.