Definition
Takes two or more arrays and returns a single array containing the unique elements that appear in any input array.
Note
Disambiguation
This page describes $setUnion when used as an expression, which combines two or more arrays into a single array of unique elements.
You can also use $setUnion in these other contexts:
$setUnion (accumulator), which returns a single array of the unique elements in the array values across a group of documents.$setUnion (window function), which is used in the$setWindowFieldsstage to return a single array of the unique elements in the array values in a window.
Syntax
{ $setUnion: [ <expression1>, <expression2>, ... ] }
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 contains only unique entries. The order of the elements in the output array is unspecified.
The arguments can be any valid expression as long as they each resolve to an array. For more information on expressions, see Expressions.
If a set contains a nested array element, $setUnion does not descend into the nested array but evaluates the array at top-level.
The following table shows the results that $setUnion returns for different inputs:
Example | Result | Notes | |||||
|---|---|---|---|---|---|---|---|
| | All elements from both arrays are combined and duplicate elements removed. | |||||
| | All elements from both arrays are combined and duplicate elements removed. | |||||
| | All elements from both arrays are combined and duplicate elements removed. | |||||
| | The result includes | |||||
| | The result consists of the unique elements in the second array because the first array is empty. | |||||
| | All elements from both arrays are combined and duplicate elements removed. |
Example
The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.
This example uses $setUnion to combine the cast and directors arrays into a single array of the people credited on each movie:
db.movies.aggregate( [ { $match: { title: { $in: [ "Annie Hall", "Reservoir Dogs" ] } } }, { $project: { _id: 0, title: 1, cast: 1, directors: 1, keyContributors: { $setUnion: [ "$cast", "$directors" ] } } }, { $sort: { title: 1 } } ] )
The preceding pipeline:
Uses
$matchto filter for two movies.Uses
$projectwith$setUnionto combinecastanddirectorsinto thekeyContributorsfield.Uses
$sortto sort results bytitle.
For Annie Hall, Woody Allen appears in both cast and directors but appears only once in keyContributors, because $setUnion removes duplicate elements.
Limitations
$setUniononly supports arrays and expressions that resolve to an array.