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

# $concatArrays (accumulator operator)

## Definition

Returns a single array that concatenates the array values of a field across a group of documents.

The `$concatArrays` 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 `$concatArrays` when used as an **accumulator**. Accumulators return a single aggregated value across a group of input documents.

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

- [`$concatArrays (expression)`](https://www.mongodb.com/docs/manual/reference/expression/concatarrays.md#mongodb-expression-exp.-concatArrays), which concatenates two or more arrays into a single array within a single document.

- [`$concatArrays (window function)`](https://www.mongodb.com/docs/manual/reference/mql/window-functions/concatarrays.md#mongodb-window-window.-concatArrays), which is used in the [`$setWindowFields`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setwindowfields.md#mongodb-pipeline-pipe.-setWindowFields) stage to concatenate the array values from a field across a window of documents.

## Syntax

```javascript
{ $concatArrays: [ <expression1>, <expression2>, ... ] }
```

## Behavior

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 following example uses the `movies` collection from the [sample\_mflix sample 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 deployment, see [Load the sample dataset.](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local)

The example uses `$concatArrays` to concatenate the `genres` arrays of movies grouped by release year:

```javascript
db.movies.aggregate( [
   {
      $match: { year: { $in: [1928, 1929] } }
   },
   {
      $group: {
         _id: "$year",
         combinedGenres: { $concatArrays: "$genres" }
      }
   },
   { $sort: { _id: 1 } }
] )

```

**Output:**

```javascript
[
  {
    _id: 1928,
    combinedGenres: [
      'Drama',   'War',
      'Drama',   'Drama',
      'History', 'War',
      'Drama',   'Animation',
      'Family',  'Comedy',
      'Drama',   'Romance',
      'Western', 'Short',
      'Drama'
    ]
  },
  {
    _id: 1929,
    combinedGenres: [
      'Drama',   'Musical',
      'Drama',   'Musical',
      'Romance', 'Biography',
      'Drama',   'History',
      'Drama',   'Musical',
      'Drama',   'Comedy',
      'Short'
    ]
  }
]

```

The operation uses [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) to select movies released in 1928 and 1929, then groups the results by `year`. The `combinedGenres` field concatenates the `genres` arrays of all movies in each year group.

## Limitations

`$concatArrays` 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/expression/concatarrays.md#mongodb-expression-exp.-concatArrays)

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