Introduction to the Aggregation Framework - MongoDB Aggregation Tutorial for beginner

Leave a Comment
Hello guy's Today we are gonna talk about mongoDB Aggregation framework. It is most powerful tool that mongodb offers us. Using aggregation framework we can group document by using specific condition. We can also add more fields during group such as Avg, Min, Max and so on. We can process collection document's in several stages one by one.

Aggregation process is very fast so aggregation query respond very fast. Aggregations operations process data records and return computed results.

Aggregation Syntax

Aggregation framework uses its special method called us aggregate(). Basic syntax of mongodb aggregation as shown below.
db.collectionName.aggregate([<stage1>,<stage2>,...<stageN>]);

Document during aggregation process and pass through the stages. Aggregate method needs one argument as a array, array of stages and each stages separated by comma.

In beginning all document pass to stage 1 then document process to stage 1 and result of processing document pass to stage 2 and stage 2 take document from stage 1 and process and result pass to stage 3 and so on. When last stage process its execution result are return back to client. Aggregation return cursor form the server.

Aggregation Process


Below image shown how mongodb aggregation works.




Above images shown overview of mongodb aggregation. In above image let suppose we have some document in collection so whole collection is shown in diagram.

First we perform match operation like we do in normal mongodb query like find, update and delete. This match query produce subset of document then we can take subset of document and perform group operation as a result of group operation we get brand new subset.

In mongodb aggregation we can group document based on certain condition and as a result we get new document and each document will represent each group.

Aggregation Pipeline


Mongodb support pipeline concept in aggregation framework. Pipeline means take set of document as input and generate a result set of documents (or the final resulting JSON document at the end of the pipeline). This can then in turn be used for the next stage and so on. Pipeline result set make documents smaller smaller and at the end we get group of documents. Again this high level diagram which shown above later in course we will dive in details.

Next Tutorial: Aggregation Stages - MongoDB Aggregation Tutorial for beginner

Please comment down below if you have any query and please follows us for more awesome tutorials and keep motivating us .

0 comments:

Post a Comment

Powered by Blogger.