Skip to content

Commit

Permalink
Merge 4.8 into 5.0 (#3061)
Browse files Browse the repository at this point in the history
  • Loading branch information
mongodb-php-bot authored Jul 22, 2024
2 parents d3fb497 + 4f2a8df commit b0bb3a5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [4.8.0] - next

* Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550)

## [4.7.0] - 2024-07-19

* Add `Query\Builder::upsert()` method by @GromNaN in [#3052](https://github.com/mongodb/laravel-mongodb/pull/3052)
Expand Down
28 changes: 28 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,40 @@ public function increment($column, $amount = 1, array $extra = [], array $option
return $this->performUpdate($query, $options);
}

public function incrementEach(array $columns, array $extra = [], array $options = [])
{
$stage['$addFields'] = $extra;

// Not using $inc for each column, because it would fail if one column is null.
foreach ($columns as $column => $amount) {
$stage['$addFields'][$column] = [
'$add' => [$amount, ['$ifNull' => ['$' . $column, 0]]],
];
}

$options = $this->inheritConnectionOptions($options);

return $this->performUpdate([$stage], $options);
}

/** @inheritdoc */
public function decrement($column, $amount = 1, array $extra = [], array $options = [])
{
return $this->increment($column, -1 * $amount, $extra, $options);
}

/** @inheritdoc */
public function decrementEach(array $columns, array $extra = [], array $options = [])
{
$decrement = [];

foreach ($columns as $column => $amount) {
$decrement[$column] = -1 * $amount;
}

return $this->incrementEach($decrement, $extra, $options);
}

/** @inheritdoc */
public function chunkById($count, callable $callback, $column = '_id', $alias = null)
{
Expand Down
50 changes: 50 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -998,4 +998,54 @@ public function testStringableColumn()
$user = DB::collection('users')->where($ageColumn, 29)->first();
$this->assertEquals('John Doe', $user['name']);
}

public function testIncrementEach()
{
DB::collection('users')->insert([
['name' => 'John Doe', 'age' => 30, 'note' => 5],
['name' => 'Jane Doe', 'age' => 10, 'note' => 6],
['name' => 'Robert Roe', 'age' => null],
]);

DB::collection('users')->incrementEach([
'age' => 1,
'note' => 2,
]);
$user = DB::collection('users')->where('name', 'John Doe')->first();
$this->assertEquals(31, $user['age']);
$this->assertEquals(7, $user['note']);

$user = DB::collection('users')->where('name', 'Jane Doe')->first();
$this->assertEquals(11, $user['age']);
$this->assertEquals(8, $user['note']);

$user = DB::collection('users')->where('name', 'Robert Roe')->first();
$this->assertSame(1, $user['age']);
$this->assertSame(2, $user['note']);

DB::collection('users')->where('name', 'Jane Doe')->incrementEach([
'age' => 1,
'note' => 2,
], ['extra' => 'foo']);

$user = DB::collection('users')->where('name', 'Jane Doe')->first();
$this->assertEquals(12, $user['age']);
$this->assertEquals(10, $user['note']);
$this->assertEquals('foo', $user['extra']);

$user = DB::collection('users')->where('name', 'John Doe')->first();
$this->assertEquals(31, $user['age']);
$this->assertEquals(7, $user['note']);
$this->assertArrayNotHasKey('extra', $user);

DB::collection('users')->decrementEach([
'age' => 1,
'note' => 2,
], ['extra' => 'foo']);

$user = DB::collection('users')->where('name', 'John Doe')->first();
$this->assertEquals(30, $user['age']);
$this->assertEquals(5, $user['note']);
$this->assertEquals('foo', $user['extra']);
}
}

0 comments on commit b0bb3a5

Please sign in to comment.