Aggregation Framework Operators¶
On this page
New in version 2.2.
Pipeline Operators¶
Note
The aggregation pipeline cannot operate
on values of the following types: Symbol
, MinKey
, MaxKey
,
DBRef
, Code
, and CodeWScope
.
Pipeline operators appear in an array. Documents pass through the operators in a sequence.
Name | Description |
---|---|
$project |
Reshapes a document stream. $project can rename, add, or remove fields as well as create computed values and sub-documents. |
$match |
Filters the document stream, and only allows matching documents to pass into the next pipeline stage. $match uses standard MongoDB queries. |
$limit |
Restricts the number of documents in an aggregation pipeline. |
$skip |
Skips over a specified number of documents from the pipeline and returns the rest. |
$unwind |
Takes an array of documents and returns them as a stream of documents. |
$group |
Groups documents together for the purpose of calculating aggregate values based on a collection of documents. |
$sort |
Takes all input documents and returns them in a stream of sorted documents. |
$geoNear |
Returns an ordered stream of documents based on proximity to a geospatial point. |
Expression Operators¶
Expression operators calculate values within the Pipeline Operators.
$group
Operators¶
Name | Description |
---|---|
$addToSet |
Returns an array of all the unique values for the selected field among for each document in that group. |
$first |
Returns the first value in a group. |
$last |
Returns the last value in a group. |
$max |
Returns the highest value in a group. |
$min |
Returns the lowest value in a group. |
$avg |
Returns an average of all the values in a group. |
$push |
Returns an array of all values for the selected field among for each document in that group. |
$sum |
Returns the sum of all the values in a group. |
Boolean Operators¶
These operators accept Booleans as arguments and return Booleans as results.
The operators convert non-Booleans to Boolean values according to the
BSON standards. Here, null
, undefined
, and 0
values become
false
, while non-zero numeric values, and all other types, such as
strings, dates, objects become true
.
Name | Description |
---|---|
$and |
Returns true only when all values in its input array are true. |
$or |
Returns true when any value in its input array are true. |
$not |
Returns the boolean value that is the opposite of the input value. |
Comparison Operators¶
These operators perform comparisons between two values and return a Boolean, in most cases reflecting the result of the comparison.
All comparison operators take an array with a pair of values. You may
compare numbers, strings, and dates. Except for $cmp
,
all comparison operators return a Boolean value. $cmp
returns an integer.
Name | Description |
---|---|
$cmp |
Compares two values and returns the result of the comparison as an integer. |
$eq |
Takes two values and returns true if the values are equivalent. |
$gt |
Takes two values and returns true if the first is larger than the second. |
$gte |
Takes two values and returns true if the first is larger than or equal to the second. |
$lt |
Takes two values and returns true if the second value is larger than the first. |
$lte |
Takes two values and returns true if the second value is larger than or equal to the first. |
$ne |
Takes two values and returns true if the values are not equivalent. |
Arithmetic Operators¶
Arithmetic operators support only numbers.
Name | Description |
---|---|
$add |
Computes the sum of an array of numbers. |
$divide |
Takes two numbers and divides the first number by the second. |
$mod |
Takes two numbers and calculates the modulo of the first number divided by the second. |
$multiply |
Computes the product of an array of numbers. |
$subtract |
Takes two numbers and subtracts the second number from the first. |
String Operators¶
String operators manipulate strings within projection expressions.
Name | Description |
---|---|
$concat |
Concatenates two strings. |
$strcasecmp |
Compares two strings and returns an integer that reflects the comparison. |
$substr |
Takes a string and returns portion of that string. |
$toLower |
Converts a string to lowercase. |
$toUpper |
Converts a string to uppercase. |
Date Operators¶
Date operators take a “Date” typed value as a single argument and return a number.
Name | Description |
---|---|
$dayOfYear |
Converts a date to a number between 1 and 366. |
$dayOfMonth |
Converts a date to a number between 1 and 31. |
$dayOfWeek |
Converts a date to a number between 1 and 7. |
$year |
Converts a date to the full year. |
$month |
Converts a date into a number between 1 and 12. |
$week |
Converts a date into a number between 0 and 53 |
$hour |
Converts a date into a number between 0 and 23. |
$minute |
Converts a date into a number between 0 and 59. |
$second |
Converts a date into a number between 0 and 59. May be 60 to account for leap seconds. |
$millisecond |
Returns the millisecond portion of a date as an integer between 0 and 999. |