Skip to content
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

Added two methods incrementEach and decrementEach #2550

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
34 changes: 34 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,46 @@ public function increment($column, $amount = 1, array $extra = [], array $option
return $this->performUpdate($query, $options);
}

public function incrementEach(array $columns, array $extra = [], array $options = [])
{
$query = ['$inc' => $columns];

if ($extra) {
$query['$set'] = $extra;
}

// Protect
foreach ($columns as $column => $amount) {
$this->where(function ($query) use ($column) {
$query->where($column, 'exists', false);

$query->orWhereNotNull($column);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With cumulative conditions on all fields, no field will be incremented if at least 1 of the fields is null.

We'd be wise not to set any conditions at all and let the server return an error.

});
}

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

return $this->performUpdate($query, $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->assertNull($user['age']);
$this->assertArrayNotHasKey('note', $user);

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']);
}
}
Loading