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 用户变量名称可以包含ascii字符[_a-zA-Z0-9]
and any non-ascii character.[_a-zA-Z0-9]
和任何非ascii字符。
User variable names must begin with a lowercase ascii letter 用户变量名称必须以小写ascii字母[a-z]
or a non-ascii character.[a-z]
或非ascii字符开头。
System Variables系统变量
MongoDB offers the following system variables:MongoDB提供以下系统变量:
NOW | NOW |
CLUSTER_TIME | CLUSTER_TIME CLUSTER_TIME |
ROOT | |
CURRENT | CURRENT the same as ROOT .CURRENT 开头,与ROOT 相同。CURRENT is modifiable. CURRENT 是可修改的。$<field> is equivalent to $$CURRENT.<field> , rebinding CURRENT changes the meaning of $ accesses. $<field> 相当于$$CURRENT.<field> ,重新绑定CURRENT 将更改$ 访问的含义。 |
REMOVE | $project , a field set to the variable REMOVE is excluded from the output.$project 中,设置为变量REMOVE 的字段将从输出中排除。 |
DESCEND | $redact expression.$redact 表达式允许的结果之一。 |
PRUNE | $redact expression.$redact 表达式允许的结果之一。 |
KEEP | $redact expression.$redact 表达式允许的结果之一。 |
SEARCH_META | $$SEARCH_META returns the metadata results for the query.$$SEARCH_META 的字段将返回查询的元数据结果。facet 和count 。 |
USER_ROLES | USER_ROLES , see the find, aggregation, view, updateOne, updateMany, and findAndModify examples. USER_ROLES 的用例,请参阅find 、aggregation 、视图、updateOne 、updateMany 和findAndModify 示例。 |