$setUnion (aggregation)
On this page
Definition
-
$setUnion
-
New in version 2.6.
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 |
---|---|
Example
Consider an experiments
collection with the following documents:
The following operation uses the $setUnion
operator to return an array of elements found in the A
array or the B
array or both:
The operation returns the following results: