Database Manual / Aggregation Operations / Reference

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

Aggregation expressions can use both user-defined and system variables.聚合表达式可以使用用户定义变量和系统变量。

Variables can hold any BSON type data. To access the value of the variable, prefix the variable name with double dollar signs ($$); i.e. "$$<variable>".变量可以保存任何BSON类型的数据。要访问变量的值,请在变量名前加上双美元符号($$);即"$$<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描述
NOWA 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为部署的所有成员返回相同的值,并在聚合管道的所有阶段保持不变。
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.为部署的所有成员返回相同的值,并在管道的所有阶段保持不变。

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.除非另有记录,否则所有阶段都以CURRENT开头,与ROOT相同。

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 exclusion of fields in $addFields and $project stages.计算结果为缺失值的变量。允许排除$addFields$project阶段中的字段。

For examples that use $$REMOVE, see:有关使用$$REMOVE的示例,请参阅:

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 MongoDB Search facet and count.有关其用法的示例,请参阅MongoDB搜索方面计数

USER_ROLES

Returns the roles assigned to the current user.返回分配给当前用户的角色

For use cases that include USER_ROLES, see the find, aggregation, view, updateOne, updateMany, and findAndModify examples.对于包含USER_ROLES的用例,请参阅findaggregationviewupdateOneupdateManyfindAndModify示例。

Using the USER_ROLES system variable in aggregations is not supported in M0 and Flex clusters.M0和Flex集群不支持在聚合中使用USER_ROLES系统变量。

New in version 7.0.在版本7.0中新增。