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.
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.null
or missing, the operator will return null
.$concatArrays
Operator
null
, the operator returns null
, making it essential to handle these cases with conditional logic.$concatArrays
Operator
The $concatArrays
operator is used in various scenarios, including:
Let's go through some practical examples to understand how the $concatArrays operator works. In the following examples, we are working with:
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]
.
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" ]
.
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"]
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4