MongoDB Aggregation Set Operator - $setUnion
MongoDB: $setUnion
The MongoDB $setUnion operators take any number of argument expressions or arrays and return an array containing the elements that appear in any input array.
Syntax:
{ $setUnion: [ <expression1>, <expression2>, ... ] }
- The $setUnion operator can only performs set operation on arrays and treated as sets. If any duplicate value contains in an array, it ignores by $setUnion operator. The order of the elements in not important in this operation..
- Only the unique value in an array appears as a result after filtering the duplicates values in the $setUnion operation.
- When a set contains a nested array element, the $setUnion does not descend into the nested array but evaluates the array at top-level.
Sample collection test_collection
Example: $setUnion
The following aggregation operation uses the $setUnion operator compares the array A and B returns an array set that contain the common elements as well as the rest elements found in both the arrays.
After the operation, following result will be returned by the $setUnion operator.
Previous:
$setIntersection
Next:
$setDifference