[SERVER-29971] Readable Aggregation Formulas Created: 04/Jul/17  Updated: 23/Nov/17  Resolved: 23/Nov/17

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Gabriel Zimmermann Assignee: Asya Kamsky
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-29406 syntactic sugars for aggregations Closed
Backwards Compatibility: Fully Compatible
Participants:

 Description   

When writing complex aggregation queries, the expressions can become difficult to read. Perhaps being able to provide a string that can be parsed to the current syntax might help simplifying expressions.

Here is an example for a group stage:

{
  $group: {
    _id: '$field',
    foo: {
      $add: ["$field1", {$multiply: ["$field2", {$subtract: [90,"$field3"]}]}]
    }
}

Compared to this:

{
  $group: {
    _id: '$field',
    foo: "$field1 + $field2 * (90 - $field3)"
  }
}

A simple stack structure could be used to transform the latter into the former. Obviously, there are scenarios in which you couldn't use this syntax to mirror the current implementation, but that's not the intention either.

I've been going through some of our large aggregation queries and a lot of them are just formulas that could be made a lot easier to read if such a feature existed.



 Comments   
Comment by Asya Kamsky [ 23/Nov/17 ]

arg20,

Thanks for the suggestion. As you can see, SERVER-29406 which is a related request (for writing complex formulas as strings) was closed because we decided that constructing aggregation to be sent to the server belongs in the drivers or frameworks, and the server itself should be receiving the actual pipeline BSON that will be executed.

Allowing the server to accept and parse strings in the pipeline would likely introduce potential security vulnerabilities as is the case in SQL, and it may not fully resolve the readability issue as not every possible aggregation could be written as a formula.
However,

expressions can become difficult to read

This is an excellent point - looking at a pipeline does not very easily allow one to visualize it as a common looking math expression. Of course one can always include math formula as a comment with the aggregation, but that has the risk of code and its comment diverging as people make edits, so that's not a solution that one could rely on.

Whatever the solution is going to be, it will have to be implemented on the driver or client side, rather than in the server and that's why we are closing this ticket as "Won't Fix".

Asya Kamsky
Lead Product Manager, MongoDB Server

Comment by Kelsey Schubert [ 05/Jul/17 ]

Hi arg20,

Thank you for the feature request. I'm marking this ticket to be considered by the Query Team; please continue to watch for updates.

Kind regards,
Thomas

Comment by Gabriel Zimmermann [ 05/Jul/17 ]

As a side comment, while true that a driver could implement this, I think that the degree of clarification this kind of syntax brings to the table merits some consideration for the database. For $group formulas it could really simplify the way aggregations look A LOT imho.

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