# $concatArrays (aggregation)

在本页面

* [定义](#definition)
* [行为](#behavior)
* [例子](#example)

## 定义

**$concatArrays**

*3.2版中的新功能。*

连接数组以返回连接的数组。

`$concatArrays`具有以下语法：

```
{ $concatArrays: [ <array1>, <array2>, ... ] }
```

该`<array>`表达式可以是任何有效的表达式，只要它们解析为一个数组。有关表达式的更多信息，请参见表达式。

如果有任何参数解析为`null`或指向缺少的字段，则`$concatArrays`返回`null`。

## 行为

| 例子                                                                  | 结果                                       |
| ------------------------------------------------------------------- | ---------------------------------------- |
| { $concatArrays: \[ \[ "hello", " "], \[ "world" ] ] }              | \[ "hello", " ", "world" ]               |
| { $concatArrays: \[ \[ "hello", " "], \[ \[ "world" ], "again"] ] } | \[ "hello", " ", \[ "world" ], "again" ] |

## 例子

名为的集合`warehouses`包含以下文档：

```
{ "_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: [ ] }
```

以下示例将`instock`和`ordered` 数组串联在一起：

```
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" ] }
```

> 也可以看看
>
> `$push`

译者：李冠飞

校对：


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mongoing.com/can-kao/yun-suan-fu/aggregation-pipeline-operators/concatarrays-aggregation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
