A colleague asked me if it's possible to generate all combinations of 2 items for a given array using aggregation pipeline expression. In other words, something that gives this:
I'm always game to challenge aggregation so here is what I came up with for the <generate> expression for k=2:
This is the aggregation equivalent of looping over the array elements and for each one looping over the remaining array elements to create the pairs. If array is `a` it's:
Aggregation expressions are pretty powerful. I gave a talk about the power of expressions over arrays at MongoDB World and local events in 2017/2018: if you missed it watch it HERE.
Likes technical puzzles, world travel and fancy food