w3resource logo
MongoDB Aggregation Pipeline Operators SQL Tutorial

MongoDB Aggregation Pipeline Operatos

Description

The MongoDB, aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines. Documents enter as an input into multi-stage pipeline which transforms the documents into an aggregated results.

The MongoDB aggregation pipeline consists various stages. Each stage transforms the documents passes through the pipeline. In this stage it is not needed to produce one output document for every input document. The pipeline stages can appear multiple times in the pipeline.

Here is a list of MongoDB Aggregation Pipeline Operators in the table below.

Stages operators:

Name Description
$project The $project function in MongoDB passes along the documents with only the specified fields to the next stage in the pipeline. This may be the existing fields from the input documents or newly computed fields.
$match The MongoDB $match operator filters the documents to pass only those documents that match the specified condition(s) to the next pipeline stage.
$redact The $redact operator can change and gives a new form of each document in the stream by restricting the content for each document based on information stored in the documents themselves.
$unwind The MongoDB $unwind stages operator is used to deconstructs an array field from the input documents to output a document for each element. Every output document is the input document with the value of the array field replaced by the element.
$group The MongoDB $group stages operator groups the documents by some specified expression and groups the document for each distinct grouping. An _id field in the output documents contains the distinct group by key. The output documents can also contain computed fields that hold the values of some accumulator expression grouped by the $group‘s _id field. This operator does not order its output documents.
$out The MongoDB $out write the resulting document of the aggregation pipeline to a specified collection. The $out operator must be the last stage in the pipeline. The $out operator lets the aggregation framework return result sets of any size.

Set operators:

Name Description
$setEquals The MongoDB $setEquals operators compares between two or more arrays and returns true if they have the same distinct elements otherwise it returns false.
$setIntersection The MongoDB $setIntersection operators takes two or more arrays and returns a set of array with elements that appear in all of the input sets.
$setUnion The MongoDB $setUnion operators takes any number of argument expressions or arrays and returns an array containing the elements that appear in any input array.
$setDifference The MongoDB $setDifference operators takes exactly two argument expressions i.e. two sets and returns an array containing the elements that only exist in the first set; i.e. performs a relative complement of the second set relative to the first.
$setIsSubset The MongoDB $setIsSubset operators takes exactly two argument expressions i.e. two sets and returns true when the first array is a subset of the second, including when the first array equals the second array, and false otherwise.
$anyElementTrue The MongoDB $anyElementTrue operators takes a single argument expression. i.e. a set and returns true if any elements of the set evaluate to true; otherwise, returns false. An empty array returns false.
$allElementsTrue The MongoDB $allElementsTrue operators takes a single argument expression. i.e. a set and returns true if no element in the array is false. Otherwise, returns false. An empty array returns true.