Expressions are MQL components that resolve to a value without modifying their inputs. You can use them in the following contexts:
Some aggregation pipeline stages, such as
$project,$addFields, and$groupQuery predicates that use
$exprFind command projections
In the MongoDB Query Language, you can build expressions from the following components:
Component | Example |
|---|---|
Constants |
|
Operators | |
Field path expressions |
|
For example, { $add: [ 3, "$inventory.total" ] } is an expression that consists of the $add operator and two operands:
The constant
3The field path expression
"$inventory.total"
The expression returns the result of adding 3 to the value at path inventory.total of the input document.
Expression operators take an array of arguments and have the following form:
{ <operator>: [ <argument1>, <argument2> ... ] }
If an operator accepts a single argument, you can omit the outer array:
{ <operator>: <argument> }
Arithmetic Operators
Arithmetic expressions perform mathematic operations on numbers. Some arithmetic expressions can also support date arithmetic.
Name | Description |
|---|---|
Returns the absolute value of a number. | |
Adds numbers to return the sum, or adds numbers and a date to return a new date. If adding numbers and a date, treats the numbers as milliseconds. Accepts any number of argument expressions, but at most, one expression can resolve to a date. | |
Returns the smallest integer greater than or equal to the specified number. | |
Returns the result of dividing the first number by the second. Accepts two argument expressions. | |
Raises e to the specified exponent. | |
Returns the largest integer less than or equal to the specified number. | |
Calculates the natural log of a number. | |
Calculates the log of a number in the specified base. | |
Calculates the log base 10 of a number. | |
Returns the remainder of the first number divided by the second. Accepts two argument expressions. | |
Multiplies numbers to return the product. Accepts any number of argument expressions. | |
Raises a number to the specified exponent. | |
Rounds a number to to a whole integer or to a specified decimal place. | |
Returns the result of the sigmoid function (the integration of the normal distribution with standard deviation 1). | |
Calculates the square root. | |
Returns the result of subtracting the second value from the first. If the two values are numbers, return the difference. If the two values are dates, return the difference in milliseconds. If the two values are a date and a number in milliseconds, return the resulting date. Accepts two argument expressions. If the two values are a date and a number, specify the date argument first as it is not meaningful to subtract a date from a number. | |
Truncates a number to a whole integer or to a specified decimal place. |
Array Operators
Name | Description |
|---|---|
Returns the element at the specified array index. | |
Converts an array of key value pairs to a document. | |
Concatenates arrays to return the concatenated array. | |
Selects a subset of the array to return an array with only the elements that match the filter condition. | |
Returns the first element of an array. Distinct from the | |
Returns a specified number of elements from the beginning of an array. Distinct from the | |
Returns a boolean indicating whether a specified value is in an array. | |
Searches an array for an occurrence of a specified value and returns the array index of the first occurrence. Array indexes start at zero. | |
Determines if the operand is an array. Returns a boolean. | |
Returns the last element of an array. Distinct from | |
Returns a specified number of elements from the end of an array. Distinct from the | |
Applies a subexpression to each element of an array and returns the array of resulting values in order. Accepts named parameters. | |
Returns the | |
Returns the | |
Converts a document to an array of documents representing key-value pairs. | |
Outputs an array containing a sequence of integers according to user-defined inputs. | |
Applies an expression to each element in an array and combines them into a single value. | |
Returns an array with the elements in reverse order. | |
Returns the number of elements in the array. Accepts a single expression as argument. | |
Returns a subset of an array. | |
Sorts the elements of an array. | |
Returns the top | |
Merge two arrays together. |
Bitwise Operators
Name | Description |
|---|---|
Returns the result of a bitwise New in version 6.3. | |
Returns the result of a bitwise New in version 6.3. | |
Returns the result of a bitwise New in version 6.3. | |
Returns the result of a bitwise New in version 6.3. |
Boolean Operators
Boolean expressions evaluate their argument expressions as booleans and return a boolean as the result.
In addition to the false boolean value, Boolean expression evaluates as false the following: null, 0, and undefined values. The Boolean expression evaluates all other values as true, including non-zero numeric values and arrays.
Name | Description |
|---|---|
Returns | |
Returns the boolean value that is the opposite of its argument expression. Accepts a single argument expression. | |
Returns |
Comparison Operators
Comparison expressions return a boolean except for $cmp which returns a number.
The comparison expressions take two argument expressions and compare both value and type, using the specified BSON comparison order for values of different types.
Name | Description |
|---|---|
Returns | |
Returns | |
Returns | |
Returns | |
Returns | |
Returns | |
Returns |
Conditional Operators
Name | Description |
|---|---|
A ternary operator that evaluates one expression, and depending on the result, returns the value of one of the other two expressions. Accepts either three expressions in an ordered list or three named parameters. | |
Returns either the non-null result of the first expression or the result of the second expression if the first expression results in a null result. Null result encompasses instances of undefined values or missing fields. Accepts two expressions as arguments. The result of the second expression can be null. | |
Evaluates a series of case expressions. When it finds an expression which evaluates to |
Custom Aggregation Operators
Name | Description |
|---|---|
Defines a custom accumulator function. | |
Defines a custom function. |
Data Size Operators
The following operators return the size of a data element:
Name | Description |
|---|---|
Returns the size of a given string or binary data value's content in bytes. | |
Returns the size in bytes of a given document (i.e. bsontype |
Date Operators
The following operators returns date objects or components of a date object:
Name | Description |
|---|---|
Adds a number of time units to a date object. | |
Returns the difference between two dates. | |
Constructs a BSON Date object given the date's constituent parts. | |
Converts a date/time string to a date object. | |
Subtracts a number of time units from a date object. | |
Returns a document containing the constituent parts of a date. | |
Returns the date as a formatted string. | |
Truncates a date. | |
Returns the day of the month for a date as a number between 1 and 31. | |
Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday). | |
Returns the day of the year for a date as a number between 1 and 366 (leap year). | |
Returns the hour for a date as a number between 0 and 23. | |
Returns the weekday number in ISO 8601 format, ranging from | |
Returns the week number in ISO 8601 format, ranging from | |
Returns the year number in ISO 8601 format. The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601). | |
Returns the milliseconds of a date as a number between 0 and 999. | |
Returns the minute for a date as a number between 0 and 59. | |
Returns the month for a date as a number between 1 (January) and 12 (December). | |
Returns the seconds for a date as a number between 0 and 60 (leap seconds). | |
Converts value to a Date. | |
Returns the week number for a date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year). | |
Returns the year for a date as a number (e.g. 2014). |
The following arithmetic operators can take date operands:
Name | Description |
|---|---|
Adds numbers and a date to return a new date. If adding numbers and a date, treats the numbers as milliseconds. Accepts any number of argument expressions, but at most, one expression can resolve to a date. | |
Returns the result of subtracting the second value from the first. If the two values are dates, return the difference in milliseconds. If the two values are a date and a number in milliseconds, return the resulting date. Accepts two argument expressions. If the two values are a date and a number, specify the date argument first as it is not meaningful to subtract a date from a number. |
Expressions Associated with Accumulators
Name | Description |
|---|---|
Returns the average of numeric array elements. | |
Returns the maximum value from an array. | |
Returns the median (50th percentile) of an array of numeric values. New in version 7.0. | |
Returns the minimum value from an array. | |
Returns one or more percentile values computed over an array of numbers. New in version 7.0. | |
Returns the population standard deviation of numeric values in an array. |
Literal Expression Operators
Miscellaneous Operators
Name | Description |
|---|---|
Generate a new random ObjectId value. | |
Returns the value of a specified field from a document. You can use | |
Generates a binary hash value from a UTF-8 string or binary data. | |
Generates an uppercase hexadecimal hash string from a UTF-8 string or binary data. | |
Returns a random float between 0 and 1 | |
Randomly select documents at a given rate. Although the exact number of documents selected varies on each run, the quantity chosen approximates the sample rate expressed as a percentage of the total number of documents. | |
Computes and returns the hash of the input expression using the same hash function that MongoDB uses to create a hashed index. |
Object Operators
Name | Description |
|---|---|
Combines multiple documents into a single document. | |
Converts a document to an array of documents representing key-value pairs. | |
Adds, updates, or removes a specified field in a document. You can use New in version 5.0. |
Set Operators
Set expressions performs set operation on arrays, treating arrays as sets. Set expressions ignores the duplicate entries in each input array and the order of the elements.
If the set operation returns a set, the operation filters out duplicates in the result to output an array that contains only unique entries. The order of the elements in the output array is unspecified.
If a set contains a nested array element, the set expression does not descend into the nested array but evaluates the array at top-level.
Name | Description |
|---|---|
Returns | |
Returns | |
Returns a set with elements that appear in the first set but not in the second set; i.e. performs a relative complement of the second set relative to the first. Accepts exactly two argument expressions. | |
Returns | |
Returns a set with elements that appear in all of the input sets. Accepts any number of argument expressions. | |
Returns | |
Returns a set with elements that appear in any of the input sets. |
String Operators
String expressions, with the exception of
$concat, only have a well-defined behavior for strings of ASCII characters.
$concat behavior is well-defined regardless of the characters used.
Name | Description |
|---|---|
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 an occurrence of a substring and returns the UTF-8 byte index of the first occurrence. If the substring is not found, returns | |
Searches a string for an occurrence of a substring and returns the UTF-8 code point index of the first occurrence. If the substring is not found, returns | |
Removes whitespace or the specified characters from the beginning of a string. | |
Applies a regular expression (regex) to a string and returns information on the first matched substring. | |
Applies a regular expression (regex) to a string and returns information on the all matched substrings. | |
Applies a regular expression (regex) to a string and returns a boolean that indicates if a match is found or not. | |
Replaces the first instance of a matched string in a given input. | |
Replaces all instances of a matched string in a given input. | |
Removes whitespace or the specified characters from the end of a string. | |
Splits a string into substrings based on a delimiter. Returns an array of substrings. If the delimiter is not found within the string, returns an array containing the original string. | |
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: | |
Deprecated. Use | |
Returns the substring of a string. Starts with the character at the specified UTF-8 byte index (zero-based) in the string and continues for the specified number of bytes. | |
Returns the substring of a string. Starts with the character at the specified UTF-8 code point (CP) index (zero-based) in the string and continues for the number of code points specified. | |
Converts a string to lowercase. Accepts a single argument expression. | |
Converts value to a string. | |
Removes whitespace or the specified characters from the beginning and end of a string. | |
Converts a string to uppercase. Accepts a single argument expression. |
Encrypted String Operators
Encrypted string expressions evaluate an argument against an encrypted field in a collection with Queryable Encryption enabled, and return a boolean.
Name | Description |
|---|---|
Returns | |
Returns | |
Returns | |
Returns |
Text Operators
Name | Description |
|---|---|
Access available per-document metadata related to the aggregation operation. |
Timestamp Operators
Timestamp expression operators return values from a timestamp.
Trigonometry Operators
Trigonometry expressions perform trigonometric operations on numbers. Values that represent angles are always input or output in radians. Use $degreesToRadians and $radiansToDegrees to convert between degree and radian measurements.
Name | Description |
|---|---|
Returns the sine of a value that is measured in radians. | |
Returns the cosine of a value that is measured in radians. | |
Returns the tangent of a value that is 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 | |
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 that is measured in radians. | |
Returns the hyperbolic cosine of a value that is measured in radians. | |
Returns the hyperbolic tangent of a value that is measured in radians. | |
Converts a value from degrees to radians. | |
Converts a value from radians to degrees. |
Type Operators
Name | Description |
|---|---|
Converts a value to a specified type. | |
Converts a value to an array. | |
Converts value to a boolean. | |
Converts value to a Date. | |
Converts value to a Decimal128. | |
Converts value to a double. | |
Converts value to an integer. | |
Converts value to a long. | |
Converts a string to an object. | |
Converts value to an ObjectId. | |
Converts value to a string. | |
Return the BSON data type of the field. | |
Converts a string to a UUID. |
Variable Operators
Name | Description |
|---|---|
Defines variables for use within the scope of a subexpression and returns the result of the subexpression. Accepts named parameters. Accepts any number of argument expressions. |
For operators used in the $setWindowFields stage, see Window Functions.
Vector Similarity Operators
Name | Description |
|---|---|
Returns the cosine similarity between two numeric vectors. Accepts an optional | |
Returns the dot product of two numeric vectors. Accepts an optional | |
Returns the Euclidean distance between two numeric vectors. Accepts an optional |