[SERVER-20284] Allow $cond to accept longer arrays Created: 03/Sep/15 Updated: 06/Dec/22 |
|
| Status: | Backlog |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Minor - P4 |
| Reporter: | Kevin Perry | Assignee: | Backlog - Query Optimization |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | expression | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Assigned Teams: |
Query Optimization
|
| Participants: |
| Description |
|
Why not allow the array form of $cond to accept longer arrays of if-then-elseif-then conditions? Instead of restricting it to only accepting arrays of length 3, it could accept any array of length 2*n + 1, with obvious interpretation: { "$cond": [ if, then, elseif, then, elseif, then, ..., else ] }. This is clearer and simpler than having to nest multiple $cond terms. |
| Comments |
| Comment by Asya Kamsky [ 19/Jul/17 ] | |||||
|
I think this is more a request for terser version of switch when the same value is being compared to different possible values. For the record, DECODE is something like this:
This is like a C switch statement, rather than $switch/$case we implemented already which evaluates completely independent case statements. | |||||
| Comment by Kevin Perry [ 05/Jan/17 ] | |||||
|
Yes, it's true the "meaning" of the "3rd element" is different for a 3-element array than for a longer array, because there is always the trailing "else" element. But if you know you're writing an if-then-else, there's really only one reasonable decoding of what an array of any particular length means: the 2*n + 1 terms are used as n "if-else" pairs, plus a trailing "else" expression. This, along with the fact that the array length must be odd, can be made clear in the doc. The behavior would actually be somewhat similar to that of Oracle's DECODE() function, which has been in use for decades (https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm). | |||||
| Comment by Asya Kamsky [ 05/Jan/17 ] | |||||
|
I think if we leave it open we would have to make it clear what the desired syntax for $cond could be. Currently $cond accepts one of the following syntax:
Adding to array would raise an ambiguity of what each subsequent array element means (if there's a 4th element, what is it? In your example is would be last else but third element would be different than in the 3-element array case vs 4-array case which is confusing to read). Using named keys is problematic because you cannot have the same keyname twice (so you can't have multiple 'else-if' etc). | |||||
| Comment by Kevin Perry [ 05/Jan/17 ] | |||||
|
Well, sort of. Using $switch is certainly cleaner than the alternative of using multiple embedded $conds. But extending $cond as I've suggested would give an even cleaner, more compact way of writing the same statements, if desired. Most languages do implement both "switch" and "elseif" constructs. Unless you have some particular reason not to, it still seems like this would be a nice feature to consider adding. | |||||
| Comment by Asya Kamsky [ 04/Jan/17 ] | |||||
|
|