Aggregation Pipeline Operators in MongoDB


1- Pipeline Aggregation Stages

To retrieve data according to user MongoDB provide aggregate operators, using these operators we can reshape array, sort the records, filter the records, etc. 

Pipeline stages appear in an array. Documents pass through the stages in sequence.


db.collection.aggregate( [ { <stage> }, ... ] )


$project: This operator use to reshape each document for the next pipeline such as adding a new field removing the existing field. Continue...

$match: This the operator uses to filters the documents stream. If specific condition(s) the match then it will allow documenting to pass the next pipeline.  Continue...

$redact: This operator use to restrict the content of document based on information stored in the documents themselves(by MongoDB) you may say to change and give a new form of each document in the stream by restricting the content for each document based on information stored in the documents themselves.  Continue...

$limit: This operator uses to pass n documents to the next pipeline where n is the specified limit. Continue...

$skip: This operator use to skip first to n documents and remaining will be passed in the next pipeline. n= skip number.  Continue...

$unwind: De-constructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.  More detail with example.

$group: This the operator is used to groups the documents by some specified expression and group the documents for each distinct grouping. According to MongoDB doc "Groups 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" Continue..

$sort: This operator use to sort on the specified key it changes only order without modifying the document. Continue...

$geoNear: This operator use to retrieve data based on proximity to a geospatial point. The output documents include an additional distance field and can include a location identifier field. More detail with example.

$out: This operator use to 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. Continue...

$lookup: This is a new feature of MongoDB version 3.2
$lookup perform the left outer joint between two collections in same database. For each input document, $lookup adds new array fields whose element matches the joined collection. Continue...

2-Boolean Aggregation Operators

$and, $or and $not operator is known as a Boolean aggregate operator in MongoDB.

$and: This operator returns the value if it’s all expression evaluate true. Its take any number of expression. It is the same as AND operator in SQL Server. Continue...

$or: This operator returns the value of one expression true from it’s all expression. Its takes any number of expression. $or operator is the same as like OR operator in SQL Server. Continue...

$not: This operator returns the Boolean value. $not return the Boolean value to the opposite of its expression. Continue...
3-Set Operator Aggregate 

Guys if you are aware of SET theory in mathematics then you can understand easily Set Operator Aggregation. Set operators assume to array-like sets and perform the set operation on arrays. The operation returns a unique entry sets after filter all the duplicates. The order of the elements in the output array is unspecified. If the set contains a nested array element, the expression evaluates it on the top level.

Name and description of set operators

$setEquals:  Its returns true or false, if the input sets (array) have the same distinct element returns true if not then false. $setEquals accept two or more expressions. Continue...

$setIntersection: Its returns common elements appear in all input sets and accept any number of documents.

$setUnion: Its returns the all element set appears in input sets and accept any number of expression. Continue...

$setDifference: It accepts only two argument expression and returns the set of element who appears in first, not in second. Continue...

$setIsSubset: Its returns true or false, $setIsSubset accepts only two argument expressions only and returns true if all elements of the first expression belong to the second expression. Continue...

$anyElementTrue: Its returns true or false $anyElementTrue accepts only one argument expression and returns true if evaluate expression true otherwise false. Continue...

$allElementsTrue:  Its returns true or false, $allElementsTrue accepts only one argument expression and returns true if no element of a set evaluate to false. Continue...

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...