티스토리 뷰

Database/mongoDB

[MongoDB] Aggregation Operators

데브포유 2017. 3. 21. 23:19
반응형

Aggregation Pipeline Operators

NOTE

For details on specific operator, including syntax and examples, click on the specific operator to go to its reference page.

Stage Operators

In the db.collection.aggregate method, pipeline stages appear in an array. Documents pass through the stages in sequence.

db.collection.aggregate( [ { <stage> }, ... ] )
NameDescription
$collStatsReturns statistics regarding a collection or view.
$projectReshapes each document in the stream, such as by adding new fields or removing existing fields. For each input document, outputs one document.
$matchFilters the document stream to allow only matching documents to pass unmodified into the next pipeline stage. $match uses standard MongoDB queries. For each input document, outputs either one document (a match) or zero documents (no match).
$redactReshapes each document in the stream by restricting the content for each document based on information stored in the documents themselves. Incorporates the functionality of $project and $match. Can be used to implement field level redaction. For each input document, outputs either one or zero documents.
$limitPasses the first n documents unmodified to the pipeline where n is the specified limit. For each input document, outputs either one document (for the first ndocuments) or zero documents  (after the first n documents).
$skipSkips the first n documents where n is the specified skip number and passes the remaining documents unmodified to the pipeline. For each input document, outputs either zero documents (for the first n documents) or one document (if after the first n documents).
$unwindDeconstructs an array field from the input documents to output a document for each element. Each output document replaces the array with an element value. For each input document, outputs n documents where n is the number of array elements and can be zero for an empty array.
$groupGroups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group. Consumes all input documents and outputs one document per each distinct group. The output documents only contain the identifier field and, if specified, accumulated fields.
$sampleRandomly selects the specified number of documents from its input.
$sortReorders the document stream by a specified sort key. Only the order changes; the documents remain unmodified. For each input document, outputs one document.
$geoNearReturns an ordered stream of documents based on the proximity to a geospatial point. Incorporates the functionality of $match$sort, and $limit for geospatial data. The output documents include an additional distance field and can include a location identifier field.
$lookupPerforms a left outer join to another collection in the same database to filter in documents from the “joined” collection for processing.
$outWrites the resulting documents of the aggregation pipeline to a collection. To use the $out stage, it must be the last stage in the pipeline.
$indexStatsReturns statistics regarding the use of each index for the collection.
$facetProcesses multiple aggregation pipelines within a single stage on the same set of input documents. Enables the creation of multi-faceted aggregations capable of characterizing data across multiple dimensions, or facets, in a single stage.
$bucketCategorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries.
$bucketAutoCategorizes incoming documents into a specific number of groups, called buckets, based on a specified expression. Bucket boundaries are automatically determined in an attempt to evenly distribute the documents into the specified number of buckets.
$sortByCountGroups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
$addFieldsAdds new fields to documents. Outputs documents that contain all existing fields from the input documents and newly added fields.
$replaceRootReplaces a document with the specified embedded document. The operation replaces all existing fields in the input document, including the _id field. Specify a document embedded in the input document to promote the embedded document to the top level.
$countReturns a count of the number of documents at this stage of the aggregation pipeline.
$graphLookupPerforms a recursive search on a collection. To each output document, adds a new array field that contains the traversal results of the recursive search for that document.

Expression Operators

These expression operators are available to construct expressions for use in the aggregation pipeline.

Operator expressions are similar to functions that take arguments. In general, these expressions take an array of arguments and have the following form:

{ <operator>: [ <argument1>, <argument2> ... ] }

If operator accepts a single argument, you can omit the outer array designating the argument list:

{ <operator>: <argument> }

To avoid parsing ambiguity if the argument is a literal array, you must wrap the literal array in a $literal expression or keep the outer array that designates the argument list.

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: null0, and undefined values. The Boolean expression evaluates all other values as true, including non-zero numeric values and arrays.

NameDescription
$andReturns true only when all its expressions evaluate to true. Accepts any number of argument expressions.
$orReturns true when any of its expressions evaluates to true. Accepts any number of argument expressions.
$notReturns the boolean value that is the opposite of its argument expression. Accepts a single argument expression.

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.

NameDescription
$setEqualsReturns true if the input sets have the same distinct elements. Accepts two or more argument expressions.
$setIntersectionReturns a set with elements that appear in all of the input sets. Accepts any number of argument expressions.
$setUnionReturns a set with elements that appear in any of the input sets. Accepts any number of argument expressions.
$setDifferenceReturns 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.
$setIsSubsetReturns true if all elements of the first set appear in the second set, including when the first set equals the second set; i.e. not a strict subset. Accepts exactly two argument expressions.
$anyElementTrueReturns true if any elements of a set evaluate to true; otherwise, returns false. Accepts a single argument expression.
$allElementsTrueReturns true if no element of a set evaluates to false, otherwise, returns false. Accepts a single argument expression.

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.

NameDescription
$cmpReturns: 0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second.
$eqReturns true if the values are equivalent.
$gtReturns true if the first value is greater than the second.
$gteReturns true if the first value is greater than or equal to the second.
$ltReturns true if the first value is less than the second.
$lteReturns true if the first value is less than or equal to the second.
$neReturns true if the values are not equivalent.

Arithmetic Operators

Arithmetic expressions perform mathematic operations on numbers. Some arithmetic expressions can also support date arithmetic.

NameDescription
$absReturns the absolute value of a number.
$addAdds 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.
$ceilReturns the smallest integer greater than or equal to the specified number.
$divideReturns the result of dividing the first number by the second. Accepts two argument expressions.
$expRaises e to the specified exponent.
$floorReturns the largest integer less than or equal to the specified number.
$lnCalculates the natural log of a number.
$logCalculates the log of a number in the specified base.
$log10Calculates the log base 10 of a number.
$modReturns the remainder of the first number divided by the second. Accepts two argument expressions.
$multiplyMultiplies numbers to return the product. Accepts any number of argument expressions.
$powRaises a number to the specified exponent.
$sqrtCalculates the square root.
$subtractReturns 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.
$truncTruncates a number to its integer.

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.

NameDescription
$concatConcatenates any number of strings.
$indexOfBytesSearches a string for an occurence of a substring and returns the UTF-8 byte index of the first occurence. If the substring is not found, returns -1.
$indexOfCPSearches a string for an occurence of a substring and returns the UTF-8 code point index of the first occurence. If the substring is not found, returns -1.
$splitSplits 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.
$strLenBytesReturns the number of UTF-8 encoded bytes in a string.
$strLenCPReturns the number of UTF-8 code points in a string.
$strcasecmpPerforms case-insensitive string comparison and returns: 0 if two strings are equivalent, 1 if the first string is greater than the second, and -1 if the first string is less than the second.
$substrDeprecated. Use $substrBytes or $substrCP.
$substrBytesReturns 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.
$substrCPReturns 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.
$toLowerConverts a string to lowercase. Accepts a single argument expression.
$toUpperConverts a string to uppercase. Accepts a single argument expression.

Text Search Operators

NameDescription
$metaAccess text search metadata.

Array Operators

NameDescription
$arrayElemAtReturns the element at the specified array index.
$concatArraysConcatenates arrays to return the concatenated array.
$filterSelects a subset of the array to return an array with only the elements that match the filter condition.
$indexOfArraySearches an array for an occurence of a specified value and returns the array index of the first occurence. If the substring is not found, returns -1.
$isArrayDetermines if the operand is an array. Returns a boolean.
$rangeOutputs an array containing a sequence of integers according to user-defined inputs.
$reverseArrayReturns an array with the elements in reverse order.
$reduceApplies an expression to each element in an array and combines them into a single value.
$sizeReturns the number of elements in the array. Accepts a single expression as argument.
$sliceReturns a subset of an array.
$zipMerge two lists together.
$inReturns a boolean indicating whether a specified value is in an array.

Variable Operators

NameDescription
$mapApplies a subexpression to each element of an array and returns the array of resulting values in order. Accepts named parameters.
$letDefines variables for use within the scope of a subexpression and returns the result of the subexpression. Accepts named parameters.

Literal Operators

NameDescription
$literalReturn a value without parsing. Use for values that the aggregation pipeline may interpret as an expression. For example, use a $literal expression to a string that starts with a $ to avoid parsing as a field path.

Date Operators

NameDescription
$dayOfYearReturns the day of the year for a date as a number between 1 and 366 (leap year).
$dayOfMonthReturns the day of the month for a date as a number between 1 and 31.
$dayOfWeekReturns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).
$yearReturns the year for a date as a number (e.g. 2014).
$monthReturns the month for a date as a number between 1 (January) and 12 (December).
$weekReturns 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).
$hourReturns the hour for a date as a number between 0 and 23.
$minuteReturns the minute for a date as a number between 0 and 59.
$secondReturns the seconds for a date as a number between 0 and 60 (leap seconds).
$millisecondReturns the milliseconds of a date as a number between 0 and 999.
$dateToStringReturns the date as a formatted string.
$isoDayOfWeekReturns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).
$isoWeekReturns the week number in ISO 8601 format, ranging from 1 to 53. Week numbers start at 1 with the week (Monday through Sunday) that contains the year’s first Thursday.
$isoWeekYearReturns 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).

Conditional Expressions

NameDescription
$condA 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.
$ifNullReturns 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.
$switchEvaluates a series of case expressions. When it finds an expression which evaluates to true$switch executes a specified expression and breaks out of the control flow.

Data Type Expressions

NameDescription
$typeReturn the BSON data type of the field.

Accumulators

Changed in version 3.2: Some accumulators are now available in the $project stage. In previous versions of MongoDB , accumulators are available only for the $group stage.

Accumulators, when used in the $group stage, maintain their state (e.g. totals, maximums, minimums, and related data) as documents progress through the pipeline.

When used in the $group stage, accumulators take as input a single expression, evaluating the expression once for each input document, and maintain their stage for the group of documents that share the same group key.

When used in the $project stage, the accumulators do not maintain their state. When used in the $project stage, accumulators take as input either a single argument or multiple arguments.

NameDescription
$sum

Returns a sum of numerical values. Ignores non-numeric values.

Changed in version 3.2: Available in both $group and $project stages.

$avg

Returns an average of numerical values. Ignores non-numeric values.

Changed in version 3.2: Available in both $group and $project stages.

$first

Returns a value from the first document for each group. Order is only defined if the documents are in a defined order.

Available in $group stage only.

$last

Returns a value from the last document for each group. Order is only defined if the documents are in a defined order.

Available in $group stage only.

$max

Returns the highest expression value for each group.

Changed in version 3.2: Available in both $group and $project stages.

$min

Returns the lowest expression value for each group.

Changed in version 3.2: Available in both $group and $project stages.

$push

Returns an array of expression values for each group.

Available in $group stage only.

$addToSet

Returns an array of unique expression values for each group. Order of the array elements is undefined.

Available in $group stage only.

$stdDevPop

Returns the population standard deviation of the input values.

Changed in version 3.2: Available in both $group and $project stages.

$stdDevSamp

Returns the sample standard deviation of the input values.

Changed in version 3.2: Available in both $group and $project stages.


반응형