- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Array Aggregation Operators >
- $concatArrays (aggregation)
$concatArrays (aggregation)¶
On this page
Definition¶
- $concatArrays¶
New in version 3.2.
Concatenates arrays to return the concatenated array.
$concatArrays has the following syntax:
{ $concatArrays: [ <array1>, <array2>, ... ] }
The <array> expressions can be any valid expression as long as they resolve to an array. For more information on expressions, see Expressions.
If any argument resolves to a value of null or refers to a field that is missing, $concatArrays returns null.
Behavior¶
Example | Results |
---|---|
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } | [ "hello", " ", "world" ] |
{ $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } | [ "hello", " ", [ "world" ], "again" ] |
Example¶
A collection named warehouses contains the following documents:
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
The following example concatenates the instock and the ordered arrays:
db.warehouses.aggregate([
{ $project: { items: { $concatArrays: [ "$instock", "$ordered" ] } } }
])
{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] }
{ "_id" : 2, "items" : null }
{ "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] }
{ "_id" : 4, "items" : [ "ice cream" ] }
See also
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.