> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $setUnion (accumulator operator)

## Definition

Returns an array of the unique elements in the array values across a group of documents.

**New in version 8.1**

The `$setUnion` accumulator is available in these stages:

- [`$bucket`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucket.md#mongodb-pipeline-pipe.-bucket)

- [`$bucketAuto`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucketauto.md#mongodb-pipeline-pipe.-bucketAuto)

- [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group)

**Note: Disambiguation**

This page describes `$setUnion` when used as an **accumulator**. Accumulators return a single aggregated value across a group of input documents.

You can also use `$setUnion` in these other contexts:

- [`$setUnion (expression)`](https://www.mongodb.com/docs/manual/reference/expression/setunion.md#mongodb-expression-exp.-setUnion), which combines two or more arrays into a single array of unique elements.

- [`$setUnion (window function)`](https://www.mongodb.com/docs/manual/reference/mql/window-functions/setunion.md#mongodb-window-window.-setUnion), which is used in the [`$setWindowFields`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setwindowfields.md#mongodb-pipeline-pipe.-setWindowFields) stage to return a single array of the unique elements in the array values in a window.

## Syntax

```javascript
{ $setUnion: "<array field>" }
```

## 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.

If a set contains a nested array element, `$setUnion` does not descend into the nested array but evaluates the array at top-level.

To see how `$setUnion` treats nested array elements, see the examples in the [`$setUnion (expression)`](https://www.mongodb.com/docs/manual/reference/expression/setunion.md#mongodb-expression-exp.-setUnion) section.

Arguments that are `null` or resolve to `null` are included in the results. Arguments that refer to a field that is missing are not included in the results.

## Example

The examples on this page use data from the [sample\_mflix dataset](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix). For details on how to load this dataset into your self-managed MongoDB deployment, see [Load the Sample Dataset](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local). 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 return the distinct genres that three specific directors have worked in:

```javascript
db.movies.aggregate( [
   {
      $unwind: "$directors"
   },
   {
      $match: {
         directors: {
            $in: [ "Christopher Nolan", "Sofia Coppola", "Spike Lee" ]
         }
      }
   },
   {
      $group: {
         _id: "$directors",
         movieCount: { $sum: 1 },
         genres: { $setUnion: "$genres" }
      }
   },
   {
      $sort: { _id: 1 }
   }
] )

```

**Output:**

```javascript
[
  {
    _id: 'Christopher Nolan',
    movieCount: 9,
    genres: [
      'Crime',
      'Action',
      'Drama',
      'Sci-Fi',
      'Mystery',
      'Thriller',
      'Adventure'
    ]
  },
  {
    _id: 'Sofia Coppola',
    movieCount: 5,
    genres: [ 'History', 'Romance', 'Drama', 'Biography', 'Comedy', 'Crime' ]
  },
  {
    _id: 'Spike Lee',
    movieCount: 27,
    genres: [
      'Action',
      'Comedy',
      'Drama',
      'Crime',
      'Documentary',
      'Music',
      'Sport',
      'Biography',
      'Romance',
      'History',
      'Mystery'
    ]
  }
]

```

The preceding pipeline:

- Uses [`$unwind`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unwind.md#mongodb-pipeline-pipe.-unwind) to create a separate document for each director credit.

- Uses [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) to filter for three specific directors.

- Uses [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) to group by director, count the movies with [`$sum`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sum.md#mongodb-group-grp.-sum), and combine the `genres` arrays of all of the director's movies into a single array of unique genres with `$setUnion`.

- Uses [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) to sort results by director name.

## Limitations

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

## Learn More

- [Accumulators](https://www.mongodb.com/docs/manual/reference/mql/accumulators.md#std-label-accumulators-ref)

- [`$concatArrays`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/concatarrays.md#mongodb-group-grp.-concatArrays)

- [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group)
