Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 17bc7e6

Browse files
DOCSP-41741: incrementEach and decrementEach (#3088)
* DOCSP-41741: incrementEach and decrementEach * clarify v4.8 * edits
1 parent 9b29d25 commit 17bc7e6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

‎docs/includes/query-builder/QueryBuilderTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,20 @@ public function testIncrement(): void
555555
$this->assertIsInt($result);
556556
}
557557

558+
public function testIncrementEach(): void
559+
{
560+
// begin increment each
561+
$result = DB::table('movies')
562+
->where('title', 'Lost in Translation')
563+
->incrementEach([
564+
'awards.wins' => 2,
565+
'imdb.votes' => 1050,
566+
]);
567+
// end increment each
568+
569+
$this->assertIsInt($result);
570+
}
571+
558572
public function testDecrement(): void
559573
{
560574
// begin decrement
@@ -566,6 +580,20 @@ public function testDecrement(): void
566580
$this->assertIsInt($result);
567581
}
568582

583+
public function testDecrementEach(): void
584+
{
585+
// begin decrement each
586+
$result = DB::table('movies')
587+
->where('title', 'Dunkirk')
588+
->decrementEach([
589+
'metacritic' => 1,
590+
'imdb.rating' => 0.4,
591+
]);
592+
// end decrement each
593+
594+
$this->assertIsInt($result);
595+
}
596+
569597
public function testPush(): void
570598
{
571599
// begin push

‎docs/query-builder.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,23 @@ the ``imdb.votes`` field in the matched document:
11251125
The ``increment()`` query builder method returns the number of documents that the
11261126
operation updated.
11271127

1128+
Starting in {+odm-short+} v4.8, you can also use the ``incrementEach()`` query
1129+
builder method to increment multiple values in a single operation. The following
1130+
example uses the ``incrementEach()`` method to increase the values of the ``awards.wins``
1131+
and ``imdb.votes`` fields in the matched document:
1132+
1133+
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
1134+
:language: php
1135+
:dedent:
1136+
:start-after: begin increment each
1137+
:end-before: end increment each
1138+
1139+
.. note::
1140+
1141+
If you pass a field to the ``increment()`` or ``incrementEach()`` method that
1142+
has no value or doesn't exist in the matched documents, these methods initialize
1143+
the specified field to the increment value.
1144+
11281145
.. _laravel-mongodb-query-builder-decrement:
11291146

11301147
Decrement a Numerical Value Example
@@ -1143,6 +1160,23 @@ matched document:
11431160
The ``decrement()`` query builder method returns the number of documents that the
11441161
operation updated.
11451162

1163+
Starting in {+odm-short+} v4.8, you can also use the ``decrementEach()`` query builder
1164+
method to decrement multiple values in a single operation. The following example uses
1165+
the ``decrementEach()`` method to decrease the values of the ``metacritic`` and ``imdb.rating``
1166+
fields in the matched document:
1167+
1168+
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
1169+
:language: php
1170+
:dedent:
1171+
:start-after: begin decrement each
1172+
:end-before: end decrement each
1173+
1174+
.. note::
1175+
1176+
If you pass a field to the ``decrement()`` or ``decrementEach()`` method that
1177+
has no value or doesn't exist in the matched documents, these methods initialize
1178+
the specified field to the decrement value.
1179+
11461180
.. _laravel-mongodb-query-builder-push:
11471181

11481182
Add an Array Element Example

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /