Aggregation Framework Operators

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.
$redact Restricts the content of a returned document on a per-field level.
$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.
$out Writes documents from the pipeline to a collection. The $out operator must be the last stage in the pipeline.

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.

Set Operators:-

These operators provide operations on sets.

Name Description
$setEquals Returns true if two sets have the same elements.
$setIntersection Returns the common elements of the input sets.
$setDifference Returns elements of a set that do not appear in a second set.
$setUnion Returns a set that holds all elements of the input sets.
$setIsSubset Returns true if all elements of a set appear in a second set.
$anyElementTrue Returns true if any elements of a set evaluate to true, and false otherwise.
$allElementsTrue Returns true if all elements of a set evaluate to true, and false otherwise.

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 an array that contains two numbers or two dates and subtracts the second value from the first.

String Operators:-

String operators that manipulate strings.

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.

Text Search Operators:-

Operators to support text search.

Name Description
$meta Access metadata for $sort stage or $projectstage.

Array Operators:-

Operators that manipulate arrays.

Name Description
$size Returns the size of the array.

Projection Expressions:-

Operators that increase the flexibility within aggregation projection and projection-like expressions. These operators are available in the $project, $group, and $redact pipeline stages.

Name Description
$map Applies a sub-expression to each item in an array and returns the result of the sub-expression.
$let Defines variables for use within the scope of an aggregation expression.
$literal Forces the aggregation pipeline to return a literal value without evaluating the expression.

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.

Conditional Expressions:-

Name Description
$cond A ternary operator that evaluates one expression, and depending on the result returns the value of one following expressions.
$ifNull Evaluates an expression and returns a value.

 

  • Ask Question