[SERVER-23839] Aggregation accumulators does not allow the new "direct" array notation Created: 21/Apr/16 Updated: 06/Dec/22 Resolved: 30/May/19 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | 3.2.5 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Neil Lunn | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||||
| Assigned Teams: |
Query
|
||||||||||||||||||||||||||||||||||||||
| Operating System: | ALL | ||||||||||||||||||||||||||||||||||||||
| Steps To Reproduce: | Insert some data
Use the array expression notation to result in an array of arrays after $push
Produces an error, but $project and other operators allow it
But not accumulators of any kind;
This produces the same error due to the argument being in array notation: "aggregating group operators are unary" So in order to get an "array" you still need $map:
Which yields an array in response, but since it is not directly notated as such the expression is allowed. Desired output from $push
|
||||||||||||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||||||||||||
| Description |
|
MongoDB 3.2 allows direct notation of arrays as an expression, and is usually fine in most constructs. ie.
However, when you attempt this notation as an argument to $push you get an error:
So the case still requires transposition of elements using $map as an argument to $push in order to get the desired result:
Where $push is actually happy that the expression can be evaluated, even though the returned element is an array. Though the error itself is self explanatory and there are cases where this would avert undesired output, it is not really consistent with the usage in other constructs. It might therefore be desirable to just allow the array expression to be applied, but then only possibly error for things like $sum, or maybe even in that case just produce the 0 return value. So overall unsure if it is best to error in such a case or just produce what may be unexpected results if the usage was not intended that way. For cases where the intended result was indeed array content to be sent to the accumulator, then there is an inconsistency with usage in other places. |
| Comments |
| Comment by Asya Kamsky [ 30/May/19 ] | |||||||||||||||||||||||||||||
|
It seems like in spirit this is similar (or flip side) of problem described in SERVER-16284 so I'm closing it as duplicate of that ticket. | |||||||||||||||||||||||||||||
| Comment by Charlie Swanson [ 02/May/16 ] | |||||||||||||||||||||||||||||
Yes, this would specify an array literal with two elements. The other example (wrapping ["$x", "$y"] in another set of brackets), would be an array literal with one element, which is an array. I'm not sure I'm following your latest comment. Would you mind elaborating on the feature request? | |||||||||||||||||||||||||||||
| Comment by Neil Lunn [ 24/Apr/16 ] | |||||||||||||||||||||||||||||
|
Then again, considering something like:
Makes sense in that syntax and result, but that does bring a feature request to mind as well. | |||||||||||||||||||||||||||||
| Comment by Neil Lunn [ 24/Apr/16 ] | |||||||||||||||||||||||||||||
|
I can live with that. The notation makes logical sense as long as it's consistent. So
Actually has a nice feel to it. So just to confirm, with something like the proposed syntax in SERVER-9377, you would then be directly notating an array since that accumulator accepts two arguments?:
Or would it still make more sense to include the explicit notation?:
I've been watching SERVER-9377 since it does address some specific problem cases not currently addressed in single aggregation statements. So it would be nice to get that right, and handle expressions consistently. I actually think the latter makes more sense here and would keep it consistent with usage in other expressions. | |||||||||||||||||||||||||||||
| Comment by Charlie Swanson [ 21/Apr/16 ] | |||||||||||||||||||||||||||||
|
This is a legitimate request, but we will have to be careful about it. As proposed in The way this works for expression is somewhat weird, but I find it makes sense if you think about it. For expressions which take only one argument, you are allowed to omit wrapping the one argument in an array literal. For those that take more than one argument, you must specify the arguments in an array. This leads to this somewhat odd behavior:
We might have to / want to do the same thing for accumulators, so that if you actually wanted an array, you'd have to wrap it in another array. |