Rewrites the $addToSet + $size distinct count, which the guard refuses because the array must fit in one 16MB document, into a grouped count that spills to disk instead:
$addToSet
$size
[{$group: {_id: null, r: {$addToSet: "$region"}}}, {$project: {n: {$size: "$r"}}}] -> [{$match: {region: {$exists: true}}}, {$group: {_id: "$region"}}, {$count: "n"}]
Requires a global group holding that one accumulator, with the array read exactly once.
Rewrites the
$addToSet+$sizedistinct count, which the guard refuses because the array must fit in one 16MB document, into a grouped count that spills to disk instead:[{$group: {_id: null, r: {$addToSet: "$region"}}}, {$project: {n: {$size: "$r"}}}] -> [{$match: {region: {$exists: true}}}, {$group: {_id: "$region"}}, {$count: "n"}]
Requires a global group holding that one accumulator, with the array read exactly once.