Variables in Aggregation Expressions
On this page
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>"
.
If the variable references an object, to access a specific field in the
object, use the dot notation; i.e. "$$<variable>.<field>"
.
User Variables
User variable names can contain the ascii characters [_a-zA-Z0-9]
and any non-ascii character.
User variable names must begin with a lowercase ascii letter [a-z]
or a non-ascii character.
System Variables
MongoDB offers the following system variables:
Variable | Description |
---|---|
A variable that returns the current datetime value.
New in version 4.2. | |
A variable that returns the current timestamp value.
New in version 4.2. | |
References the root document, i.e. the top-level document, currently
being processed in the aggregation pipeline stage. | |
References the start of the field path being processed in the
aggregation pipeline stage. Unless documented otherwise, all
stages start with
| |
A variable which evaluates to the missing value. Allows for the
conditional exclusion of fields. In a For an example of its usage, see Conditionally Exclude Fields. | |
One of the allowed results of a $redact expression. | |
One of the allowed results of a $redact expression. | |
One of the allowed results of a $redact expression. | |
A variable that stores the metadata results of an Atlas
Search query. In all supported aggregation
pipeline stages, a field set to the variable
For an example of its usage, see Atlas Search facet and count. |