- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $setUnion (aggregation)
$setUnion (aggregation)¶
On this page
Definition¶
-
$setUnion
¶ Takes two or more arrays and returns an array containing the elements that appear in any input array.
$setUnion
has the following syntax:The arguments can be any valid expression as long as they each resolve to an array. For more information on expressions, see Expressions.
Behavior¶
$setUnion
performs set operation on arrays, treating arrays
as sets. If an array contains duplicate entries, $setUnion
ignores the duplicate entries. $setUnion
ignores the order of
the elements.
$setUnion
filters out duplicates in its result to output an
array that contain only unique entries. The order of the elements in
the output array is unspecified.
If a set contains a nested array element, $setUnion
does not descend
into the nested array but evaluates the array at top-level.
Example | Result |
---|---|