MongoDB $concatArrays Operator
MongoDB provides a set of operators for manipulating and transforming data within its powerful aggregation framework. One of the most useful array expression operators is the $concatArrays
operator. This operator enables developers to merge multiple arrays into a single, unified array.
Whether we're working with arrays stored in different fields, documents, or combining static arrays with dynamic data, the $concatArrays
operator simplifies the process, making it easier to manage array data in our MongoDB collections.
What is MongoDB $concatArrays Operator?
The $concatArrays
operator is a powerful tool in MongoDB’s aggregation pipeline that allows us to concatenate multiple arrays. It takes two or more arrays and combines them into a single array, preserving the order of the elements. This is particularly useful when we need to merge data from different sources or fields within a single document.
Syntax:
{ $concatArrays: [ <array1>, <array2>, ... <arrayN>] }
Key Terms
<array1>, <array2>, ..., <arrayN>
: These are the arrays to be concatenated. Each of these arrays must be a valid expression that resolves to an array.- The operator will return a single array that contains the elements of all input arrays, in the order provided.
- If any of the input arrays are
null
or missing, the operator will returnnull
.
Key Features of the $concatArrays
Operator
- Concatenation of Multiple Arrays: The operator can merge any number of arrays, regardless of their sizes or data types.
- Array Preservation: It preserves the order of elements from the original arrays.
- Handling Missing Arrays: When arrays are missing or
null
, the operator returnsnull
, making it essential to handle these cases with conditional logic. - Versatility: It works well with both arrays stored in different fields and static arrays defined inline.
Use Cases for MongoDB $concatArrays
Operator
The $concatArrays
operator is used in various scenarios, including:
- Combining Data from Multiple Fields: Merging arrays from different fields in a single document.
- Concatenating Arrays Across Different Documents: Aggregating arrays from different documents into one.
- Adding Static Arrays to Existing Data: Concatenating predefined static arrays with arrays from your documents.
- Flattening Data: Flattening complex nested structures by concatenating arrays.
Examples of MongoDB $concatArrays
Let's go through some practical examples to understand how the $concatArrays operator works. In the following examples, we are working with:
- Database: GeeksforGeeks
- Collection: arrayExample
- Document: three documents that contain the details in the form of field-value pairs.

Example 1: Using $concatArrays Operator
In this example, we are going to concatenate the values(i.e., arrays) of numbers1 and numbers2 fields using $concatArrays operator.
Query:
db.arrayExample.aggregate([
... {$match: {name: "Lolo"}},
... {$project: {
... concatResult: {$concatArrays: ["$numbers1", "$numbers2"]}}}])
Output:

Explanation: The numbers1
array contains [2, 4, 5, 6, 7, 8, 29]
, and the numbers2
array contains [2, 4, 5, 6]
. The $concatArrays
operator combines them into a single array: [2, 4, 5, 6, 7, 8, 29, 2, 4, 5 ,6]
.
Example 2: Using $concatArrays Operator to concatearrays Values
In this example, we are going to concatenate the fruits and veggie arrays into a single array for a document with name: "Bongo" using $concatArrays operator.
Query:
db.arrayExample.aggregate([
... {$match: {name: "Bongo"}},
... {$project: {concatResult:
... {$concatArrays: ["$fruits", "$veggie"]}}}])
Output:

Explanation: The fruits
array contains ["Mango", "Apple", "Banana", "Peach"]
, and the veggie
array contains ["Broccoli", "Tomato", "Pumpkin"]
. The $concatArrays
operator combines them into the concatResult
array: ["Mango", "Apple", "Banana", "Peach", "Broccoli", "Tomato", "Pumpkin" ]
.
Example 3: Using $concatArrays Operator in the Embedded Document
In this example, we are going to concatenate the value(i.e., array) of favGame.indoorGames field with the specified array(i.e., ["Uno", "Snooker"]) using $concatArrays operator.
Query:
db.arrayExample.aggregate([{$match: {name: "Piku"}},
... {$project: {concatResult: {
... $concatArrays: ["$favGame.indoorGames", ["Uno", "Snooker"]]}}}])
Output:

Explanation: The favGame.indoorGames
array contains ["Ludo", "Chess"]
. The static array ["Uno", "Snooker"]
is concatenated to it, resulting in the array: ["Chess", "Ludo", "Uno", "Snooker"]
Conclusion
The MongoDB $concatArrays
operator is a powerful tool for combining arrays in aggregation pipelines, making it an essential feature for developers working with array data. Whether we're merging data from different fields or documents, or adding static arrays to dynamic data, $concatArrays
simplifies array manipulation in MongoDB. By mastering this operator, we can enhance your ability to work with complex data structures, improve your data processing workflows, and optimize our aggregation pipelines for better performance.