For AI agents: a documentation index is available at https://www.mongodb.com/zh-cn/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

$setUnion (expression)

$setUnion

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: [ <expression1>, <expression2>, ... ]
}

$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
{ $setUnion: [
[ "a", "c" ],
[ "a", "b" ]
] }
[ "a", "c", "b" ]

All elements from both arrays are combined and duplicate elements removed.

{ $setUnion: [
[ "a", "c" ],
[ "a", "b", "c" ]
] }
[ "a", "c", "b" ]

All elements from both arrays are combined and duplicate elements removed.

{ $setUnion: [
[ "a", "b", "a" ],
[ "b", "a" ]
] }
[ "a", "b" ]

All elements from both arrays are combined and duplicate elements removed.

{ $setUnion: [
[ "a", "b" ],
[ [ "a", "b" ] ]
] }
[ "a", "b", [ "a", "b" ] ]

The result includes "a" and "b" as individual string elements and the array [ "a", "b" ] as a unique element.

{ $setUnion: [
[ ],
[ "a", "b" ]
] }
[ "a", "b" ]

The result consists of the unique elements in the second array because the first array is empty.

{ $setUnion: [
[ "a", "a" ],
[ "a", "b" ]
] }
[ "a", "b" ]

All elements from both arrays are combined and duplicate elements removed.

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 $match to filter for two movies.

  • Uses $project with $setUnion to combine cast and directors into the keyContributors field.

  • Uses $sort to sort results by title.

For Annie Hall, Woody Allen appears in both cast and directors but appears only once in keyContributors, because $setUnion removes duplicate elements.

  • $setUnion only supports arrays and expressions that resolve to an array.