> 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/aggregation/aggregation-reference/variables-in-aggregation-expressions.md).

# 聚合表达式中的变量

在本页面

* [用户变量](#user-variables)
* [系统变量](#system-variables)

[聚合表达式](https://github.com/mongodb-china/MongoDB-CN-Manual/tree/8490376c81d56eff95abbaddc6ee414b1e1c9705/docs/Aggregation/Aggregation-Reference/meta-aggregation-quick-reference.html#aggregation-expressions)可以同时使用 user-defined 和系统变量。

变量可以容纳任何[BSON 类型数据](https://github.com/mongodb-china/MongoDB-CN-Manual/tree/8490376c81d56eff95abbaddc6ee414b1e1c9705/docs/Aggregation/Aggregation-Reference/reference-bson-types.html)。要访问变量的 value，请使用带有前缀为 double 美元符号(`$$`)的变量 name 的 string。

如果变量 references 一个 object，要访问 object 中的特定字段，请使用点表示法; 即： `"$$<variable>.<field>"`。

## 用户变量

用户变量名称可以包含 ascii 字符`[_a-zA-Z0-9]`和任何 non-ascii 字符。

用户变量名必须以小写的 ascii 字母`[a-z]`或 non-ascii 字符开头。

## 系统变量

MongoDB 提供以下系统变量：

| 变量        | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ROOT`    | References 根文档，即： top-level 文档，当前正在聚合管道阶段中处理。                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `CURRENT` | Reference 聚合管道阶段中正在处理的字段路径的开始。除非另有说明，否则所有阶段都以[CURRENT](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)开头，与[ROOT](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)相同。 [CURRENT](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)可以修改。但是，由于`$<field>`等同于`$$CURRENT.<field>`，因此重新绑定[CURRENT](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)会改变`$`访问的含义。 |
| `REMOVE`  | 一个变量，用于计算缺少的 value。允许条件排除字段。在`$projection`中，从输出中排除设置为变量[REMOVE](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)的字段。 有关其用法的示例，请参阅[有条件地排除字段](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)。 version 3.6 中的新内容。                                                                                                                                                                                              |
| `DESCEND` | [$redact](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)表达式的允许结果之一。                                                                                                                                                                                                                                                                                                                                                                    |
| `PRUNE`   | [$redact](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)表达式的允许结果之一。                                                                                                                                                                                                                                                                                                                                                                    |
| `KEEP`    | [$redact](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)表达式的允许结果之一。                                                                                                                                                                                                                                                                                                                                                                    |

> **也可以看看**
>
> [$let](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)，[$redact](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)，[$map](/aggregation/aggregation-reference/variables-in-aggregation-expressions.md)

译者：李冠飞

校对：


---

# 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/aggregation/aggregation-reference/variables-in-aggregation-expressions.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.
