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

Conversation

SmallRuralDog
Copy link
Contributor

add incrementEach
add decrementEach

@GromNaN
Copy link
Member

GromNaN commented Aug 25, 2023

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 QueryBuilderTest and update the changelog?

$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.

Copy link
Member

@alcaeus alcaeus left a 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
@GromNaN GromNaN requested review from a team as code owners July 22, 2024 09:19
@GromNaN GromNaN self-requested a review July 22, 2024 09:19
@GromNaN GromNaN changed the base branch from 4.1 to 4.8 July 22, 2024 09:21
@GromNaN GromNaN requested a review from alcaeus July 22, 2024 09:45
@GromNaN
Copy link
Member

GromNaN commented Jul 22, 2024

PR updated with tests.

Copy link
Member

@alcaeus alcaeus left a 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.

$query->orWhereNotNull($column);
});
$stage['$addFields'][$column] = [
'$add' => [['$ifNull' => ['$' . $column, 0]], $amount],
Copy link
Member

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.

Suggested change
'$add' => [['$ifNull' => ['$' . $column, 0]], $amount],
'$add' => [$amount, ['$ifNull' => ['$' . $column, 0]]],

Copy link
Member

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.

@GromNaN GromNaN merged commit 4f2a8df into mongodb:4.8 Jul 22, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants