[DOCS-11089] $sum aggregation operator may accept multiple expressions when nested in $group stage Created: 06/Dec/17  Updated: 22/Jan/24

Status: Backlog
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Jeremy Mikola Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: backlog, reopened, request, server-docs-bug-bash, triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 1 year, 14 weeks ago
Story Points: 0.5

 Description   

Documentation for the $sum aggregation operator discusses two modes:

  • In a $group pipeline stage, $sum takes a single expression argument
  • In a $project pipeline stage, $sum accepts either a single expression or an array of expressions

In doctrine/mongodb-odm#1678 (and a Stack Overflow thread linked therein), we see that the server accepts the following:

[{
    "$group": {
        "_id": null,
        "games": { "$sum": { "$sum": [ "$wins", "$losses", "$ties" ] } }
    }
}]

Since $sum isn't being used in a $project stage, I would not expect it to accept multiple arguments. I'm therefore wondering if the server is being lax in what it accepts, or if the documentation is missing some nuance. It would be good to ask a server engineer to chime in on this to clarify.

Comment from kay.kim:

I guess we could clarify that if your <expression> is itself a $sum expression, then, the nested $sum expression syntax can be stuff.

As an aside, I'm not sure why $sum was overloaded to be used in $project when we already had a $add operator.



 Comments   
Comment by Sarah Olson [ 02/Nov/22 ]

Thanks jmikola@mongodb.com, asya.kamsky@mongodb.com. Appreciate the feedback.

asya.kamsky@mongodb.com: Definitely not expecting you to ping the ticket to keep it from being closed. On the Server docs team, we're trying really hard to address the large documentation ticket backlog and then implement a prioritization strategy to keep it from getting so large in the future. This way we'll be able to triage and groom our backlog periodically and efficiently, going forward. Please bear with us as we work through this very old backlog, and of course let us know if tickets have been closed that shouldn't have been!

Comment by Asya Kamsky [ 02/Nov/22 ]

I'm not sure what "due to inactivity" means - are we supposed to periodically ping the ticket to keep it from being closed?

This is related to accumulators and expressions and how they are documented. It should be considered together with DOCSP-14384

Comment by Jeremy Mikola [ 01/Nov/22 ]

The current syntax documentation seems more clear than when I originally opened this issue, so I'm fine with leaving this closed.

cc: asya.kamsky@mongodb.com in case your previous comment warrants some action (although it seems beyond the scope of just docs)

Comment by Education Bot [ 31/Oct/22 ]

Hello! This ticket has been closed due to inactivity. If you believe this ticket is still important, please reopen it and leave a comment to explain why. Thank you!

Comment by Asya Kamsky [ 01/Oct/19 ]

The real issue is that we try to enumerate stages where "$sum" is available.

Possibly a better structure for agg would be to get rid of the enumeration everywhere and just have expressions (which can be used everywhere) and accumulators which can be used in grouping stages).

Most accumulators can be used as expressions - they take one argument as accumulator, but an array of arguments as an expression.

Comment by Asya Kamsky [ 01/Oct/19 ]

> I'm not sure why $sum was overloaded to be used in $project when we already had a $add operator.

$sum as expression acts the same as accumulator $sum in that it ignores all non-numeric arguments. $add will error on non-numeric (and non-dates in some cases).

Generated at Thu Feb 08 08:02:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.