Aggregation

<< Click to Display Table of Contents >>

Current:  Create Calculated Field > Mongo Calculated Field Functions List 

Aggregation

Previous pageReturn to chapter overviewNext page

Functions

Syntax

Instructions

Examples

addToSet

{ $addToSet: <expression> }

Returns an array of all unique values that results from applying an expression to each document in a group of documents that share the same group by key. Order of the elements in the output array is unspecified.

For example:

{ $addToSet: "$col" };

avg

{ $avg: <expression> }

Returns the average value of the numeric values.

For example:

{ $avg: "$col" }

first

{ $first: <expression> }

Returns the value that results from applying an expression to the first document in a group of documents that share the same group by key. Only meaningful when documents are in a defined order.

For example:

{ $first: "$col" };

last

{ $last: <expression> }

Returns the value that results from applying an expression to the last document in a group of documents that share the same group by a field. Only meaningful when documents are in a defined order.

For example:

{ $last: "$col" };

max

{ $max: <expression> }

Returns the maximum value. $max compares both value and type, using the specified BSON comparison order for values of different types.

For example:

{ $max: [ 5, 8 ] }

mergeObjects

{ $mergeObjects: <document> }

Returns a group of multiple documents.Note: The object here is a specific structure in mongo.

For example:

{ $mergeObjects: "$object" }

min

{ $min: <expression> }

Returns the minimum value. $min compares both value and type, using the specified BSON comparison order for values of different types.

For example:

{ $min: [ 5, 8 ] }

push

{ $push: <expression> }

Returns an array of all values that result from applying an expression to each document in a group of documents that share the same group by key.

For example:

{ $push: { t1: "test1", t2: "test2" } }

stdDevPop

{ $stdDevPop: <expression> }

Returns the population standard deviation of the input values.

For example:

{ $stdDevPop: "$col" }

stdDevSamp

{ $stdDevSamp: <expression> }

Calculates the sample standard deviation of the input values. Use if the values encompass a sample of a population of data from which to generalize about the population. $stdDevSamp ignores non-numeric values.

For example:

{ $stdDevSamp: "$col" }

sum

{ $sum: <expression> }

Calculates and returns the sum of numeric values. $sum ignores non-numeric values.

For example:

{ $sum: "$col" }