Skip to content

DOCSP-41741: incrementEach and decrementEach #3088

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 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions docs/includes/query-builder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,20 @@ public function testIncrement(): void
$this->assertIsInt($result);
}

public function testIncrementEach(): void
{
// begin increment each
$result = DB::table('movies')
->where('title', 'Lost in Translation')
->incrementEach([
'awards.wins' => 2,
'imdb.votes' => 1050,
]);
// end increment each

$this->assertIsInt($result);
}

public function testDecrement(): void
{
// begin decrement
Expand All @@ -543,6 +557,20 @@ public function testDecrement(): void
$this->assertIsInt($result);
}

public function testDecrementEach(): void
{
// begin decrement each
$result = DB::table('movies')
->where('title', 'Dunkirk')
->decrementEach([
'metacritic' => 1,
'imdb.rating' => 0.4,
]);
// end decrement each

$this->assertIsInt($result);
}

public function testPush(): void
{
// begin push
Expand Down
34 changes: 34 additions & 0 deletions docs/query-builder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,23 @@ the ``imdb.votes`` field in the matched document:
The ``increment()`` query builder method returns the number of documents that the
operation updated.

Starting in {+odm-short+} v4.8, you can also use the ``incrementEach()`` query
builder method to increment multiple values in a single operation. The following
example uses the ``incrementEach()`` method to increase the values of the ``awards.wins``
and ``imdb.votes`` fields in the matched document:

.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
:language: php
:dedent:
:start-after: begin increment each
:end-before: end increment each

.. note::

If you pass a field to the ``increment()`` or ``incrementEach()`` method that
has no value or doesn't exist in the matched documents, these methods initialize
the specified field to the increment value.

.. _laravel-mongodb-query-builder-decrement:

Decrement a Numerical Value Example
Expand All @@ -1086,6 +1103,23 @@ matched document:
The ``decrement()`` query builder method returns the number of documents that the
operation updated.

Starting in {+odm-short+} v4.8, you can also use the ``decrementEach()`` query builder
method to decrement multiple values in a single operation. The following example uses
the ``decrementEach()`` method to decrease the values of the ``metacritic`` and ``imdb.rating``
fields in the matched document:

.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
:language: php
:dedent:
:start-after: begin decrement each
:end-before: end decrement each

.. note::

If you pass a field to the ``decrement()`` or ``decrementEach()`` method that
has no value or doesn't exist in the matched documents, these methods initialize
the specified field to the decrement value.

.. _laravel-mongodb-query-builder-push:

Add an Array Element Example
Expand Down
Loading