Nov 10, 2015

Aggregation Pipeline Operators in MongoDB

1- Pipeline Aggregation Stages

To retrieve data according to user MongoDB provide aggregate operators, using this 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 the each document for the next pipeline such as adding new field removing existing field. Continue...

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

$redact: This operator use to restrict content of document based on information stored in the documents themselves(by MongoDB) you may say to change and give 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 use to pass n documents to next pipe line where n is the specified limit. Continue...

$skip: This operator use to skip first to n documents and remaining will be passed in 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 operator is use 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 specified key it change only order without modify 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 new feature of MongoDB version 3.2

$lookup perform the left outer joint between two collections in same database. For the each input document $lookup add a new array fields whose element matches from joined collection. Continue...

2-Boolean Aggregation Operators



$and, $or and $not operator is known as 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 same like AND operator in SQL Server. Continue...

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


$not: This operator returns the Boolean value. $not return the Boolean value to opposite of its expression. Continue...


3-Set Operator Aggregate 

Guys if you are aware with SET theory in mathematics then you can understand easily about Set Operator Aggregation. Set operators assume to array like sets and perform the set operation on arrays. 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 top level.

Name and description of set operators

$setEquals:  Its returns true or false, if the input sets (array) have same distinct element returns true if not then false. $setEquals accept two or more expression. 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’s accept 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 expression only and returns true if all elements of first expression belong to 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...