Skip to content

Commit 859d62e

Browse files
committed
DOCSP-41741: incrementEach and decrementEach
1 parent 7ea9dc1 commit 859d62e

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
@@ -532,6 +532,20 @@ public function testIncrement(): void
532532
$this->assertIsInt($result);
533533
}
534534

535+
public function testIncrementEach(): void
536+
{
537+
// begin increment each
538+
$result = DB::table('movies')
539+
->where('title', 'Lost in Translation')
540+
->incrementEach([
541+
'awards.wins' => 2,
542+
'imdb.votes' => 1050,
543+
]);
544+
// end increment each
545+
546+
$this->assertIsInt($result);
547+
}
548+
535549
public function testDecrement(): void
536550
{
537551
// begin decrement
@@ -543,6 +557,20 @@ public function testDecrement(): void
543557
$this->assertIsInt($result);
544558
}
545559

560+
public function testDecrementEach(): void
561+
{
562+
// begin decrement each
563+
$result = DB::table('movies')
564+
->where('title', 'Dunkirk')
565+
->decrementEach([
566+
'metacritic' => 1,
567+
'imdb.rating' => 0.4,
568+
]);
569+
// end decrement each
570+
571+
$this->assertIsInt($result);
572+
}
573+
546574
public function testPush(): void
547575
{
548576
// begin push

docs/query-builder.txt

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

1071+
You can also use the ``incrementEach()`` query builder method to increment multiple
1072+
values in a single operation. The following example uses the ``incrementEach()``
1073+
method to increase the values of the ``awards.wins`` and ``imdb.votes`` fields in the
1074+
matched document:
1075+
1076+
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
1077+
:language: php
1078+
:dedent:
1079+
:start-after: begin increment each
1080+
:end-before: end increment each
1081+
1082+
.. note::
1083+
1084+
If you pass a field to the ``increment()`` or ``incrementEach()`` method that
1085+
has no value or doesn't exist in the matched documents, these methods initialize
1086+
the specified field with the increment value.
1087+
10711088
.. _laravel-mongodb-query-builder-decrement:
10721089

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

1106+
You can also use the ``decrementEach()`` query builder method to decrement multiple
1107+
values in a single operation. The following example uses the ``decrementEach()``
1108+
method to decrease the values of the ``metacritic`` and ``imdb.rating`` fields in the
1109+
matched document:
1110+
1111+
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
1112+
:language: php
1113+
:dedent:
1114+
:start-after: begin decrement each
1115+
:end-before: end decrement each
1116+
1117+
.. note::
1118+
1119+
If you pass a field to the ``decrement()`` or ``decrementEach()`` method that
1120+
has no value or doesn't exist in the matched documents, these methods initialize
1121+
the specified field with the decrement value.
1122+
10891123
.. _laravel-mongodb-query-builder-push:
10901124

10911125
Add an Array Element Example

0 commit comments

Comments
 (0)