> For the complete documentation index, see [llms.txt](https://docs.mongoing.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mongoing.com/can-kao/yun-suan-fu/aggregation-pipeline-operators/add-aggregation.md).

# $add (aggregation)

在本页面

* [定义](#definition)
* [例子](#examples)

## 定义

**$add**

添加数字或添加数字和日期。如果其中一个参数是 date，则$add将其他参数视为要添加到 date 的毫秒数。

$add表达式具有以下语法：

```
{ $add: [ <expression1>, <expression2>, ... ] }
```

参数可以是任何有效的表达，只要它们可以解析为所有数字或数字和日期。有关表达式的更多信息，请参阅表达式。

## 例子

以下示例使用带有以下文档的`sales`集合：

```
{ "_id" : 1, "item" : "abc", "price" : 10, "fee" : 2, date: ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "fee" : 1, date: ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5,  "fee" : 0, date: ISODate("2014-03-15T09:00:00Z") }
```

### 添加数字

以下聚合使用$project管道中的$add表达式来计算总成本：

```
db.sales.aggregate(
    [
        { $project: { item: 1, total: { $add: [ "$price", "$fee" ] } } }
    ]
)
```

该操作返回以下结果：

```
{ "_id" : 1, "item" : "abc", "total" : 12 }
{ "_id" : 2, "item" : "jkl", "total" : 21 }
{ "_id" : 3, "item" : "xyz", "total" : 5 }
```

### 在 Date 上执行加法

以下聚合使用$add表达式`billing_date`通过将`3*24*60*60000`毫秒(即：3 天)添加到`date`字段来计算：

```
db.sales.aggregate(
    [
        { $project: { item: 1, billing_date: { $add: [ "$date", 3*24*60*60000 ] } } }
    ]
)
```

该操作返回以下结果：

```
{ "_id" : 1, "item" : "abc", "billing_date" : ISODate("2014-03-04T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "billing_date" : ISODate("2014-03-04T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "billing_date" : ISODate("2014-03-18T09:00:00Z") }
```

译者：李冠飞

校对：


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
