-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
Hello, thanks for the suggestion. For reference, this methods were added by laravel/framework#45577. Would you like to rebase the PR, add tests in |
src/Query/Builder.php
Outdated
$this->where(function ($query) use ($column) { | ||
$query->where($column, 'exists', false); | ||
|
||
$query->orWhereNotNull($column); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address the comment by @GromNaN and add tests for the new functionality.
add incrementEach add decrementEach
PR updated with tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor suggestion to optimise the $add
operator, but I like that the behaviour now better matches the expectation from Laravel that all documents are updated and we gracefully handle null
values.
src/Query/Builder.php
Outdated
$query->orWhereNotNull($column); | ||
}); | ||
$stage['$addFields'][$column] = [ | ||
'$add' => [['$ifNull' => ['$' . $column, 0]], $amount], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the docs, there's an optimisation we can leverage in MongoDB 6.1 and later:
Starting in MongoDB 6.1 you can optimize the
$add
operation. To improve performance, group references at the end of the argument list.
'$add' => [['$ifNull' => ['$' . $column, 0]], $amount], | |
'$add' => [$amount, ['$ifNull' => ['$' . $column, 0]]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. It's strange that the server isn't able to apply this kind of optimisation itself.
add incrementEach
add decrementEach