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 4f2a8df

Browse files
SmallRuralDogGromNaN
andauthored
Added two methods incrementEach and decrementEach (#2550)
* Add tests on incrementEach/decrementEach * Fix incrementEach to handle null values --------- Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
1 parent d3fb497 commit 4f2a8df

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [4.8.0] - next
5+
6+
* Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550)
7+
48
## [4.7.0] - 2024年07月19日
59

610
* Add `Query\Builder::upsert()` method by @GromNaN in [#3052](https://github.com/mongodb/laravel-mongodb/pull/3052)

‎src/Query/Builder.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,12 +787,40 @@ public function increment($column, $amount = 1, array $extra = [], array $option
787787
return $this->performUpdate($query, $options);
788788
}
789789

790+
public function incrementEach(array $columns, array $extra = [], array $options = [])
791+
{
792+
$stage['$addFields'] = $extra;
793+
794+
// Not using $inc for each column, because it would fail if one column is null.
795+
foreach ($columns as $column => $amount) {
796+
$stage['$addFields'][$column] = [
797+
'$add' => [$amount, ['$ifNull' => ['$' . $column, 0]]],
798+
];
799+
}
800+
801+
$options = $this->inheritConnectionOptions($options);
802+
803+
return $this->performUpdate([$stage], $options);
804+
}
805+
790806
/** @inheritdoc */
791807
public function decrement($column, $amount = 1, array $extra = [], array $options = [])
792808
{
793809
return $this->increment($column, -1 * $amount, $extra, $options);
794810
}
795811

812+
/** @inheritdoc */
813+
public function decrementEach(array $columns, array $extra = [], array $options = [])
814+
{
815+
$decrement = [];
816+
817+
foreach ($columns as $column => $amount) {
818+
$decrement[$column] = -1 * $amount;
819+
}
820+
821+
return $this->incrementEach($decrement, $extra, $options);
822+
}
823+
796824
/** @inheritdoc */
797825
public function chunkById($count, callable $callback, $column = '_id', $alias = null)
798826
{

‎tests/QueryBuilderTest.php‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,4 +998,54 @@ public function testStringableColumn()
998998
$user = DB::collection('users')->where($ageColumn, 29)->first();
999999
$this->assertEquals('John Doe', $user['name']);
10001000
}
1001+
1002+
public function testIncrementEach()
1003+
{
1004+
DB::collection('users')->insert([
1005+
['name' => 'John Doe', 'age' => 30, 'note' => 5],
1006+
['name' => 'Jane Doe', 'age' => 10, 'note' => 6],
1007+
['name' => 'Robert Roe', 'age' => null],
1008+
]);
1009+
1010+
DB::collection('users')->incrementEach([
1011+
'age' => 1,
1012+
'note' => 2,
1013+
]);
1014+
$user = DB::collection('users')->where('name', 'John Doe')->first();
1015+
$this->assertEquals(31, $user['age']);
1016+
$this->assertEquals(7, $user['note']);
1017+
1018+
$user = DB::collection('users')->where('name', 'Jane Doe')->first();
1019+
$this->assertEquals(11, $user['age']);
1020+
$this->assertEquals(8, $user['note']);
1021+
1022+
$user = DB::collection('users')->where('name', 'Robert Roe')->first();
1023+
$this->assertSame(1, $user['age']);
1024+
$this->assertSame(2, $user['note']);
1025+
1026+
DB::collection('users')->where('name', 'Jane Doe')->incrementEach([
1027+
'age' => 1,
1028+
'note' => 2,
1029+
], ['extra' => 'foo']);
1030+
1031+
$user = DB::collection('users')->where('name', 'Jane Doe')->first();
1032+
$this->assertEquals(12, $user['age']);
1033+
$this->assertEquals(10, $user['note']);
1034+
$this->assertEquals('foo', $user['extra']);
1035+
1036+
$user = DB::collection('users')->where('name', 'John Doe')->first();
1037+
$this->assertEquals(31, $user['age']);
1038+
$this->assertEquals(7, $user['note']);
1039+
$this->assertArrayNotHasKey('extra', $user);
1040+
1041+
DB::collection('users')->decrementEach([
1042+
'age' => 1,
1043+
'note' => 2,
1044+
], ['extra' => 'foo']);
1045+
1046+
$user = DB::collection('users')->where('name', 'John Doe')->first();
1047+
$this->assertEquals(30, $user['age']);
1048+
$this->assertEquals(5, $user['note']);
1049+
$this->assertEquals('foo', $user['extra']);
1050+
}
10011051
}

0 commit comments

Comments
(0)

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