Variables in Aggregation Expressions聚合表达式中的变量

On this page本页内容

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中新增

ROOTReferences 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. 是可修改的。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 $project, a field set to the variable REMOVE is excluded from the output.$project中,设置为变量REMOVE的字段将从输出中排除。

For an example of its usage, see Conditionally Exclude Fields.有关其用法的示例,请参阅条件排除字段

DESCENDOne of the allowed results of a $redact expression.$redact表达式允许的结果之一。
PRUNEOne of the allowed results of a $redact expression.$redact表达式允许的结果之一。
KEEPOne of the allowed results of a $redact expression.$redact表达式允许的结果之一。
SEARCH_META

A variable that stores the metadata results of an Atlas Search query. 存储Atlas搜索查询元数据结果的变量。In all supported aggregation pipeline stages, a field set to the variable $$SEARCH_META returns the metadata results for the query.在所有受支持的聚合管道阶段中,设置为变量$$SEARCH_META的字段将返回查询的元数据结果

For an example of its usage, see Atlas Search facet and count.有关其用法的示例,请参阅Atlas Search facetcount

Tip提示
See also: 参阅:
←  Aggregation Commands ComparisonSQL to Aggregation Mapping Chart →