> 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/abs-aggregation.md).

# $abs (aggregation)

在本页面

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

## 定义

**$abs**

version 3.2 中的新内容。

返回数字的绝对 value。

`$abs`具有以下语法：

```
{ $abs: <number> }
```

`<number>`表达式可以是任何有效的表达，因为它解析为数字。有关表达式的更多信息，请参阅表达式。

## 行为

如果参数解析为的值或引用缺少的字段，则`$abs`返回`null`。如果参数解析为`NaN`，则`$abs`返回`NaN`。

| 例子               | 结果     |
| ---------------- | ------ |
| `{ $abs: -1 }`   | `1`    |
| `{ $abs: 1 }`    | `1`    |
| `{ $abs: null }` | `null` |

## 例子

集合`ratings`包含以下文档：

```
{ _id: 1, start: 5, end: 8 }
{ _id: 2, start: 4, end: 4 }
{ _id: 3, start: 9, end: 7 }
{ _id: 4, start: 6, end: 7 }
```

以下 example 计算`start`和`end`评级之间的差异大小：

```
db.ratings.aggregate([
   {
       $project: { delta: { $abs: { $subtract: [ "$start", "$end" ] } } }
   }
])
```

该操作返回以下结果：

```
{ "_id" : 1, "delta" : 3 }
{ "_id" : 2, "delta" : 0 }
{ "_id" : 3, "delta" : 2 }
{ "_id" : 4, "delta" : 1 }
```

译者：李冠飞

校对：


---

# 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/abs-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.
