The aggregation pipeline operators construct expressions for use in the aggregation pipeline stages. The following are the list of Aggregation Pipeline Operators.
It is used to perform arithmetic operations on numbers. Some arithmetic expression also supports data arithmetic.
The abs operator returns the absolute value of a number.
Syntax:
{ $abs: <number> }
Example:
db.score.aggregate([ { $school: { marks: { $abs: { $subtract: [ "$max", "$min" ] } } } } ])
It adds two or more numbers and a date. If one of the arguments is a date, then the date treats the other argument as milliseconds to add to the date.
Syntax:
{ $add: [ <expression1>, <expression2>, ... ] }
Example:
db.books.aggregate( [ { $project: { item: 1, total: { $add: [ "$price", "$tax" ] } } } ] )
The ceil operator returns the smallest integer that is greater than or equal to the specified number.
Syntax:
{ $ceil: <number> }
Example:
db.samples.aggregate([ { $project: { value: 1, ceilingValue: { $ceil: "$value" } } } ])
It divides one or more numbers by another and returns the result.
Syntax:
{ $divide: [ <expression1>, <expression2> ] }
Example:
db.planning.aggregate( [ { $project: { name: 1, workdays: { $divide: [ "$hours", 8 ] } } } ] )
The exp operator is used to raise Euler's number to the specified exponent and returns the result.
Syntax:
{ $exp: <exponent> }
Example:
db.accounts.aggregate( [ { $project: { effectiveRate: { $subtract: [ { $exp: "$rate"}, 1 ] } } } ] )
The floor operator returns the greatest integer less than or equal to the specified number.
Syntax:
{ $floor: <number> }
Example:
db.samples.aggregate( [ { $project: { value: 1, floorValue: { $floor: "$value" } } } ] )
The ln operator calculates the natural logarithm of a number and returns the result as a double.
Syntax:
{ $ln: <number> }
Example:
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
The log operator calculates the log of a number for the specified base and returns the result as double.
Syntax:
{ $log: [ <number>, <base> ] }
Example:
db.examples.aggregate([ { $project: { bitsNeeded: { $floor: { $add: [ 1, { $log: [ "$positiveInt", 2 ] } ] } } } } ])
The log10 operator calculates the log base 10 of a number and returns the result as a double.
Syntax:
{ $log10: <number> }
Example:
db.samples.aggregate( [ { $project: { pH: { $multiply: [ -1, { $log10: "$H3O" } ] } } } ] )
The mod operator divides one number with another and returns the remainder.
Syntax:
{ $mod: [ <expression1>, <expression2> ] }
Example:
db.planning.aggregate( [ { $project: { remainder: { $mod: [ "$hours", "$tasks" ] } } } ] )
The multiply operator gives the product of two or more numbers.
Syntax:
{ $multiply: [ <expression1>, <expression2>, ..... ] }
Example:
db.sales.aggregate( [ { $project: { date: 1, item: 1, total: { $multiply: [ "$price", "$quantity" ] } } } ] )
The pow operator raises the number to the given exponent and returns the result.
Syntax:
{ $pow: [ <number>, <exponent> ] }
Example:
db.quizzes.aggregate( [ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ] )
The round operator rounds a number to a whole integer or a specified decimal place.
Syntax:
{ $round : [ <number>, <place> ] }
Example:
db.samples.aggregate( [ { $project: { roundedValue: { $round: [ "$value", 1 ] } } } ] )
The sqrt operator returns the square root of a positive number as double.
Syntax:
{ $sqrt: <number> }
Example:
db.points.aggregate([ { $project: { distance: { $sqrt: { $add: [ { $pow: [ { $subtract: [ "$p2.y", "$p1.y" ] }, 2 ] }, { $pow: [ { $subtract: [ "$p2.x", "$p1.x" ] }, 2 ] } ] } } } } ])
The subtract operator subtracts two or more numbers to return the difference of the number.
Syntax:
{ $subtract: [ <expression1>, <expression2> ] }
Example:
db.sales.aggregate( [ { $project: { item: 1, total: { $subtract: [ { $add: [ "$price", "$fee" ] }, "$discount" ] } } } ] )
The trunc command deletes the data from the specified decimal place.
Syntax:
{ $trunc : [ <number>, <place> ] }
Example:
db.samples.aggregate( [ { $project: { truncatedValue: { $trunc: [ "$value", 1 ] } } } ] )
It returns the element at the specified array index.
Syntax:
{ $arrayElemAt: [ <array>, <idx> ] }
Example:
db.users.aggregate([ { $project: { name: 1, first: { $arrayElemAt: [ "$favorites", 0 ] }, last: { $arrayElemAt: [ "$favorites", -1 ] } } } ])
The arrayToObject operator converts an array into a single document.
Syntax:
[ [ "item", "abc123"], [ "qty", 25 ] ]
Example:
db.inventory.aggregate( [ { $project: { item: 1, dimensions: { $arrayToObject: "$dimensions" } } } ] )
The concatArrays operator joins the array to return the concatenated array.
Syntax:
{ $concatArrays: [ <array1>, <array2>, ... ] }
Example:
db.warehouses.aggregate([ { $project: { items: { $concatArrays: [ "$instock", "$ordered" ] } } } ])
The filter operator selects a subset of an array to return the result based on the specified condition.
Syntax:
{ $filter: { input: <array>, as: <string>, cond: <expression> } }
Example:
db.sales.aggregate([ { $project: { items: { $filter: { input: "$items", as: "item", cond: { $gte: [ "$$item.price", 100 ] } } } } } ])
The in operator returns a boolean indicating that the specified value is in the array or not.
Syntax:
{ $in: [ <expression>, <array expression> ] }
Example:
db.fruit.aggregate([ { $project: { "store location" : "$location", "has bananas" : { $in: [ "bananas", "$in_stock" ] } } } ])
The indexOfArray operator searches the array for the occurrence of a specified value and returns the array index of the first occurrence.
Syntax:
{ $indexOfArray: [ <array expression>, <search expression>, <start>, <end> ] }
Example:
db.inventory.aggregate( [ { $project: { index: { $indexOfArray: [ "$items", 2 ] }, } } ] )
It determines and returns a Boolean value if the operand is an Array.
Syntax:
{ $isArray: [ <expression> ] }
Example:
db.shop.aggregate( [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } )
The map operator attaches value to each item in an array and returns an array with the applied result.
Syntax:
{ $map: { input: <expression>, as: <string>, in: <expression> } }
Example:
db.grades.aggregate( [ { $project: { adjustedGrades: { $map: { input: "$quizzes", as: "grade", in: { $add: [ "$$grade", 2 ] } } } } } ] }
This operator converts a document to an array.
Syntax:
{ $objectToArray: <object> }
Example:
db.inventory.aggregate( [ { $project: { item: 1, dimensions: { $objectToArray: "$dimensions" } } } ] )
The range operator returns an array whose elements are a generated sequence of numbers.
Syntax:
{ $range: [ <start>, <end>, <non-zero step> ] }
Example:
db.distances.aggregate([{ $project: { _id: 0, city: 1, "Rest stops": { $range: [ 0, "$distance", 25 ] } } } ] )
The reduce operator applies an expression to each element in an array and combines them into a single value.
Syntax:
{ $reduce: { input: <array>, initialValue: <expression>, in: <expression> } }
Example:
db.clothes.aggregate( [ { $project: { "discountedPrice": { $reduce: { input: "$discounts", initialValue: "$price", in: { $multiply: [ "$$value", { $subtract: [ 1, "$$this" ] } ] } } } } } ] )
It returns an array with the element in reverse order.
Syntax:
{ $reverseArray: <array expression> }
Example:
db.users.aggregate( [ { $project: {name: 1, reverseFavorites: { $reverseArray: "$favorites" } } } ] )
The size operator counts and returns the total number of items in an array.
Syntax:
{ $size: <expression> }
Example:
db.books.aggregate( [ { $project: { item: 1, numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} } } } ] )
The slice operator results in a subset of an array.
Syntax:
{ $slice: [ <array>, <n> ] }
Example:
db.books.aggregate( [ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ])
The zip operator transposes an array so that the first element of the output array would be an array containing the first element of the first input array.
Syntax:
{ $zip: { inputs: [ <array expression1>, ... ], useLongestLength: <boolean>, defaults: <array expression> } }
Example:
db.matrices.aggregate([{ $project: { _id: false, transposed: { $zip: { inputs: [ { $arrayElemAt: [ "$matrix", 0 ] }, { $arrayElemAt: [ "$matrix", 1 ] }, { $arrayElemAt: [ "$matrix", 2 ] }, ] } } } } ] )