Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-11089

$sum aggregation operator may accept multiple expressions when nested in $group stage

      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.

            Assignee:
            lauren.tran@mongodb.com Lauren Tran
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:
              1 year, 24 weeks, 2 days ago