Variables in Aggregation Expressions聚合表达式中的变量
Aggregation expressions can use both user-defined and system variables.聚合表达式可以使用用户定义变量和系统变量。
Variables can hold any BSON type data. 变量可以保存任何BSON类型的数据。To access the value of the variable, prefix the variable name with double dollar signs ($$
); i.e. "$$<variable>"
.要访问变量的值,请在变量名称前加上双美元符号($$
);亦即,"$$<variable>"
。
If the variable references an object, to access a specific field in the object, use the dot notation; i.e. "$$<variable>.<field>"
.如果变量引用对象,要访问对象中的特定字段,请使用点表示法;亦即,"$$<variable>.<field>"
。
User Variables用户变量
User variable names can contain the ascii characters [_a-zA-Z0-9]
and any non-ascii character.用户变量名可以包含ascii字符[_a-zA-Z0-9]
和任何非ascii字符。
User variable names must begin with a lowercase ascii letter [a-z]
or a non-ascii character.用户变量名称必须以小写ascii字母[a-z]
或非ascii字符开头。
System Variables系统变量
MongoDB offers the following system variables:MongoDB提供以下系统变量:
Variable变量 | Description描述 |
-
NOW
|
A variable that returns the current datetime value. 返回当前日期时间值的变量。NOW returns the same value for all members of the deployment and remains the same throughout all stages of the aggregation pipeline.NOW 为部署的所有成员返回相同的值,并在聚合管道的所有阶段保持不变。
New in version 4.2.版本4.2中的新功能。
|
-
CLUSTER_TIME
|
A variable that returns the current timestamp value.返回当前时间戳值的变量。
CLUSTER_TIME is only available on replica sets and sharded clusters.仅在副本集和分片群集上可用。
CLUSTER_TIME returns the same value for all members of the deployment and remains the same throughout all stages of the pipeline.为部署的所有成员返回相同的值,并在管道的所有阶段保持不变。
New in version 4.2.版本4.2中的新功能。
|
-
ROOT
|
References the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage.引用当前正在聚合管道阶段处理的根文档,即顶级文档。 |
-
CURRENT
|
References the start of the field path being processed in the aggregation pipeline stage. 引用聚合管道阶段中正在处理的字段路径的开头。Unless documented otherwise, all stages start with CURRENT the same as ROOT .除非另有说明,否则所有阶段均以与ROOT 相同的CURRENT 开始。
CURRENT is modifiable. CURRENT 是可修改的。However, since $<field> is equivalent to $$CURRENT.<field> , rebinding CURRENT changes the meaning of $ accesses.但是,由于$<field> 等于$$CURRENT.<field> ,重新绑定CURRENT 更改$ 访问的含义。
|
-
REMOVE
|
A variable which evaluates to the missing value. 计算为缺失值的变量。Allows for the conditional exclusion of fields. 允许有条件地排除字段。In a $projection , a field set to the variable REMOVE is excluded from the output.在$projection 中,设置为变量REMOVE 的字段将从输出中排除。
For an example of its usage, see Conditionally Exclude Fields.有关其用法的示例,请参阅条件排除字段。
New in version 3.6.版本3.6中的新功能。
|
-
DESCEND
|
One of the allowed results of a $redact expression.$redact 表达式允许的结果之一。 |
-
PRUNE
|
One of the allowed results of a $redact expression.$redact 表达式允许的结果之一。 |
-
KEEP
|
One of the allowed results of a $redact expression.$redact 表达式允许的结果之一。 |