- 12 Minutes to read
Transformation Pipeline Expressions
- 12 Minutes to read
On this page, you will find a comprehensive list of the following data transformation pipeline expressions:
For more information on types of expressions, refer to the article on Transformation Pipeline.
|Returns the current datetime value. Remains constant across all stages of the aggregation pipeline.
|Provides the current timestamp in replica sets and sharded clusters. Consistent across all deployment members and throughout the pipeline stages.
|Refers to the top-level document currently in the aggregation pipeline stage.
|Points to the start of the field path being processed. It starts the same as
ROOT and is modifiable, which affects the interpretation of
$ field path accesses.
|Evaluates to a missing value to conditionally exclude fields. Used in
$project to omit fields.
|An allowable outcome of a
|Another possible result of a
$redact stage, indicating the removal of data.
|An outcome of
$redact stage indicating that the data should be retained.
Data Types and Structures
Arithmetic Expression Operators
Arithmetic expression operators are mathematical operations on numbers, with some supporting date arithmetic.
|Returns the absolute value of a number.
|Adds numbers or adds milliseconds to a date. Accepts multiple arguments with at most one date.
|Rounds up to the nearest integer.
|Divides the first number by the second. Requires two arguments.
|Raises e to a given exponent.
|Rounds down to the nearest integer.
|Computes the natural log of a number.
|Computes the log of a number for a given base.
|Computes the base 10 logarithm of a number.
|Returns the remainder after division. Requires two arguments.
|Multiplies numbers. Accepts multiple arguments.
|Raises a number to a given exponent.
|Rounds a number to the nearest integer or specified decimal place.
|Computes the square root of a number.
|Subtracts numbers, dates, or subtracts milliseconds from a date. Requires two arguments, with date first if used.
|Truncates a number to an integer or specified decimal place.
Array Expression Operators
|Retrieves the element at a specified array index.
|Transforms an array of key-value pairs into a document.
|Concatenates multiple arrays.
|Filters an array based on a condition.
|Retrieves the first array element (different from
|Checks if a value exists in an array; returns boolean.
|Finds the first occurrence of a value in an array or returns -1 if not found.
|Checks if the operand is an array; returns boolean.
|Retrieves the last array element (different from
|Applies a subexpression to each array element, returning an array of results.
|Converts a document into an array of key-value pair documents.
|Generates an array of integers based on user-defined parameters.
|Combines array elements using an expression into a single value.
|Reverses the order of array elements.
|Counts elements in an array.
|Extracts a portion of an array.
|Merges two arrays into one.
Boolean Expression Operators
Boolean expressions return a boolean result based on their arguments. They interpret
false. All other values, including non-zero numbers and arrays, are interpreted as
|Returns true if all argument expressions are true.
|Inverts the boolean value of its argument.
|Returns true if at least one of its argument expressions is true.
Comparison Expression Operators
Comparison expressions compare both value and type of two arguments, using the BSON comparison order for different types.
$cmp uniquely returns a number, while others return a boolean.
|Outputs 0 for equivalent values, 1 if the first is greater, and -1 if the first is less.
|True when values are equivalent.
|True when the first value exceeds the second.
|True when the first value is greater or matches the second.
|True when the first value is smaller than the second.
|True when the first value is smaller or matches the second.
|True when values are not equivalent.
Conditional Expression Operators
|Evaluates the first expression and, based on its result, returns the value from either the second or third expression. Can accept three ordered expressions or three named parameters.
|If the first expression is null, undefined, or missing, returns the result of the second expression. Accepts two expressions; the second one can also return null.
|Checks multiple case expressions. On the first true case, executes the corresponding expression and exits.
Custom Aggregation Expression Operators
|Allows creation of a custom accumulator for use in aggregation pipelines.
Data Size Operators
|Returns the byte size of a string or binary data value's content.
|Returns the byte size of a document when encoded as BSON (bsontype Object).
Date Expression Operators
|Constructs a BSON Date object from its constituent parts.
|Converts a date/time string to a BSON Date object.
|Returns the constituent parts of a date as a document.
|Formats the date as a string.
|Returns the day of the month as a number (1-31).
|Returns the weekday number (1 for Sunday to 7 for Saturday).
|Returns the day number of the year (1-366).
|Returns the hour component (0-23).
|Returns the weekday number in ISO 8601 format (1 for Monday to 7 for Sunday).
|Returns the ISO 8601 week number (1-53).
|Returns the year number as per ISO 8601.
|Returns the milliseconds component (0-999).
|Returns the minute component (0-59).
|Returns the month number (1 for January to 12 for December).
|Returns the seconds component (0-60).
|Converts a value to a Date. New in version 4.0.
|Returns the week number (0 for the partial week before the first Sunday to 53 for a leap year).
|Returns the year as a number.
It is possible to use the following arithmetic operators to work with date operands.
|Adds numbers to a date, treating numbers as milliseconds. At most, one argument can resolve to a date.
|Subtracts values; if both are dates, returns difference in milliseconds. With a date and number, returns the resulting date. Specify the date first when subtracting a number from it.
Literal Expression Operator
|Returns a value without interpretation. Useful for values that may be mistaken as expressions, especially strings starting with $. Prevents accidental field path parsing.
|Returns a random float between 0 and 1.
|Selects documents randomly based on a given rate. The chosen count is roughly the rate's percentage of the total documents.
Object Expression Operators
|Combines multiple documents into one.
|Converts a document into an array of documents representing key-value pairs.
Set Expression Operators
Set expressions operate on arrays, treating them as sets. They filter out any duplicate entries within each array and do not guarantee a specific order for the elements in the resulting array. Nested arrays are evaluated at the top level without delving into their individual elements.
|Evaluates if all elements in a set are true. Accepts a single argument.
|Evaluates if at least one element in a set is true. Accepts a single argument.
|Returns elements that are in the first set but not in the second, essentially performing a relative complement. Requires two arguments.
|Checks if the input sets contain the same distinct elements. Can accept two or more arguments.
|Returns elements common to all input sets. Can accept any number of arguments.
|Checks if all elements of the first set are present in the second, but not necessarily vice versa. Requires two arguments.
|Combines and returns elements from all input sets without duplicates.
String Expression Operators
String expressions primarily operate with well-defined behavior on ASCII character strings. However, the
$concat function is an exception, exhibiting consistent behavior regardless of the character set.
|Concatenates any number of strings.
|Converts a date/time string to a date object.
|Returns the date as a formatted string.
|Searches a string for a substring and returns the UTF-8 byte index of the first occurrence. Returns -1 if not found.
|Searches a string and returns the UTF-8 code point index of the first occurrence of a substring. Returns -1 if not found.
|Removes whitespace or specified characters from the beginning of a string.
|Applies a regex to a string and returns information on the first matched substring.
|Applies a regex to a string and returns information on all matched substrings.
|Applies a regex to a string and returns a boolean indicating if a match is found.
|Replaces the first instance of a matched string in the input.
|Replaces all instances of a matched string in the input.
|Removes whitespace or specified characters from the end of a string. New in version 4.0.
|Splits a string based on a delimiter and returns an array of substrings.
|Returns the number of UTF-8 encoded bytes in a string.
|Returns the number of UTF-8 code points in a string.
|Performs case-insensitive string comparison and returns 0 if equivalent, 1 if the first is greater, -1 if less.
|Returns a substring starting at a specified UTF-8 byte index and continues for a specified number of bytes.
|Returns a substring starting at a specified UTF-8 code point index and continues for a specified number of code points.
|Converts a string to lowercase.
|Converts a value to a string.
|Removes whitespace or specified characters from the start and end of a string.
|Converts a string to uppercase.
Text Expression Operators
|Retrieves specific metadata about a document during aggregation, such as the relevance score from a text search.
Trigonometry Expression Operators
Trigonometry expressions handle trigonometric operations on numerical values. When working with angles, these expressions always use radians. For conversions between degrees and radians, utilize
|Returns the sine of a value measured in radians.
|Returns the cosine of a value measured in radians.
|Returns the tangent of a value measured in radians.
|Returns the inverse sin (arc sine) of a value in radians.
|Returns the inverse cosine (arc cosine) of a value in radians.
|Returns the inverse tangent (arc tangent) of a value in radians.
|Returns the inverse tangent (arc tangent) of y / x in radians, where y and x are the first and second values passed to the expression, respectively.
|Returns the inverse hyperbolic sine (hyperbolic arc sine) of a value in radians.
|Returns the inverse hyperbolic cosine (hyperbolic arc cosine) of a value in radians.
|Returns the inverse hyperbolic tangent (hyperbolic arc tangent) of a value in radians.
|Returns the hyperbolic sine of a value measured in radians.
|Returns the hyperbolic cosine of a value measured in radians.
|Returns the hyperbolic tangent of a value measured in radians.
|Converts a value from degrees to radians.
|Converts a value from radians to degrees.
Type Expression Operators
|Converts a value to a specified type.
true if the expression resolves to an integer, decimal, double, or long; otherwise, returns
|Converts a value to a boolean.
|Converts a value to a date.
|Converts a value to Decimal128.
|Converts a value to a double.
|Converts a value to an integer.
|Converts a value to a long.
|Converts a value to an ObjectId.
|Converts a value to a string.
|Returns the BSON data type of the field.
_blank} of the aggregation pipeline, accumulators are essential operators that keep track of data states such as totals, peaks, troughs, and more. Each accumulator evaluates an expression for every document in its group. The unique feature of these operators is their ability to retain their state across documents with the same group key.
|Returns the result of a user-defined accumulator function.
|Returns an array of unique expression values for each group.
|Calculates the average of numerical values. Excludes non-numeric values.
|Retrieves a value from the first document in each group. Defined order is necessary for consistent results.
|Retrieves a value from the last document in each group. Defined order is necessary for consistent results.
|Identifies the highest expression value in each group.
|Combines input documents to create a single document for each group.
|Identifies the lowest expression value in each group.
|Produces an array of expression values for each group.
|Calculates the population standard deviation of input values.
|Calculates the sample standard deviation of input values.
|Sums up numerical values in each group. Excludes non-numeric values.
Accumulators (in Other Stages)
Some operators that are available as accumulators for the
_blank} are also available for use in other stages but not as accumulators. When used in these other stages, these operators do not maintain their state and can take as input either a single argument or multiple arguments.
The following accumulator operators are available in the
|Computes an average for the given expression or set of expressions per document, ignoring non-numeric values.
|Retrieves a value from the initial document in each group. Order matters if documents follow a specific sequence.
|Gleans a value from the last document in each group. Document sequence matters for a defined order.
|Determines the peak value from the provided expression or set of expressions for every document.
|Identifies the lowest value from the designated expression or collection of expressions per document.
|Computes the overall standard deviation from the input values.
|Calculates the sample standard deviation from the input values.
|Aggregates the sum of numerical entries, excluding non-numeric ones.
Variable Expression Operators
|Defines variables for use in a subexpression's scope and returns its result. It accepts named parameters and can take any number of argument expressions.