The $setEquals aggregation pipeline operator has the following interface:
{ $setEquals: [ <expression1>, <expression2>, ... ] }
Now after a few $group stages I have a document like this:
{ myLists: [[...], [...], [...]] }
And would like to check if all those sub-arrays contain the same values, so pass it to $setEquals:
{ $setEquals: '$myLists' }
but mongo complains: $setEquals needs at least two arguments had: 1.
If I know the number of elements, I can just pass them explicitly:
{ $setEquals: [{ $arrayElemAt: ['$myLists', 0] }, { $arrayElemAt: ['$myLists', 1] }] }
If I don't know the number, I can use $reduce to do it sort-of element-wise, but this requires the empty set as an indicator for "false":
{
$reduce: {
input: '$myList',
initialValue: { $arrayElemAt: ['$myList', 0] },
in: {
$cond: {
if: { $setEquals: ['$$value', '$$this'] },
then: '$$value',
else: [],
},
},
},
}
Is there a better way?