MongoDB $concatArrays Operator (original) (raw)

Last Updated : 5 May, 2026

The $concatArrays operator in MongoDB combines two or more arrays into a single array within the aggregation pipeline, preserving the original order of elements.

Syntax

{ $concatArrays: [ , , ... ] }

Features of the $concatArrays Operator

Here are some features of $concatArrays operator:

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:

Screenshot-2026-02-13-124751

Example 1: Using $concatArrays Operator

Concatenate the values (i.e., arrays) of numbers1 and numbers2 fields using $concatArrays operator.

db.arrayExample.aggregate([
{ $match: { name: "Lorenzo" } },
{
$project: {
concatResult: { concatArrays:["concatArrays: ["concatArrays:["numbers1", "$numbers2"] }
}
}
])

**Output:

concatenate

Example 2: Using $concatArrays Operator to concatenate arrays Values

Concatenate the fruits and veggie arrays into a single array for a document with name: "Bruno" using $concatArrays operator.

db.arrayExample.aggregate([
{ $match: { name: "Bruno" } },
{
$project: {
concatResult: { concatArrays:["concatArrays: ["concatArrays:["fruits", "$veggie"] }
}
}
])

**Output:

concatenate2

Example 3: Using $concatArrays Operator in the Embedded Document

Concatenate the value (i.e., array) of favGame.indoorGames field with the specified array(i.e., ["Uno", "Snooker"]) using $concatArrays operator.

db.arrayExample.aggregate([
{ $match: { name: "Pika" } },
{
$project: {
concatResult: {
concatArrays:["concatArrays: ["concatArrays:["favGame.indoorGames", ["Uno", "Snooker"]]
}
}
}
])

**Output:

concatenate3

Use Cases for MongoDB $concatArrays Operator

The $concatArrays operator is used in various scenarios, including: