Skip to content

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
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/includes/usage-examples/UpdateManyTest.php
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(['$set' => ['acclaimed' => true]]);

echo 'Updated documents: ' . $updates;
// end-update-many

$this->assertEquals(2, $updates);
$this->expectOutputString('Updated documents: 2');
}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ calls the controller function and returns the result to a web interface.

/usage-examples/findOne
/usage-examples/updateOne
/usage-examples/updateMany
/usage-examples/deleteOne
66 changes: 66 additions & 0 deletions docs/usage-examples/updateMany.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.. _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 an Eloquent model or query builder.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question:
Is it possible to call on the model directly? I could be wrong, but I think you need to chain it to an object collection like the result of "where()" for Laravel MongoDB.

I think this description should match what's shown in the example either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, it needs to be on an object collection - fixed


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
Comment on lines +32 to +34
Copy link
Contributor

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.


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``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
I think it could be helpful to add some information about the value printed even if it seems self-explanatory. E.g.
"The update() method returns the number of documents 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 `Updates
<https://laravel.com/docs/{+laravel-docs-version+}/eloquent#updates>`__ section of the
Laravel documentation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question:
Do you know if all the content in this section is available in Laravel MongoDB? For example, do the methods listed in the "Examining Attribute Changes" section work?

If these do not work, perhaps this link should be omitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They don't seem to work, so I'll remove