$match and $group Aggregation Stages - MongoDB Aggregation Tutorial For Beginner

Leave a Comment
After introduction of group stage let's combine $match and $group stages. So direct jump to example using our person collection.
db.getCollection("person").aggregate([ 
 {$match:{country:"China"}},   // stage 1
 {$group:{_id:{age:"$age", gender:"$gender",}}}   // stage 2
]);
In above example first we use match stage where we find all document whose country is china and next group stage and finally we will get documents with _id set to have embedded document with two filed age and gender, so remember all document in person collection go to match stage first we will filter and find only document whose country is china and then resulting document will go to group stage and group will produce brand new documents that will contain all possible combination of age and gender of the document came out from match stage. Result shown below in image.

$match and $group Aggregation Stages - MongoDB Aggregation Tutorial For Beginner

Now let's switch both stages means first we will see group stage and after that match stage so let's try below example in ROBO 3T.
db.getCollection("person").aggregate([ 
  {$group:{_id:{age:"$age", gender:"$gender",}}},   // stage 1
  {$match:{country:"China"}} // stage 2
]);
Above example's result will be empty because order of aggregation stage is wrong. In above query first stage output will be _id with embedded document of all possible combination of age and gender after that in second stage we are trying to filter document whose country is China but in this example first stage only contain _id fields as output so when it pass to second stage and try to match with country we get empty result. So in aggregation, order of aggregation execution is also important to get expected result.

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

$group Aggregation Stages - MongoDB Aggregation Tutorial For Beginner

Leave a Comment
In Last tutorial we have seen $match stage now we will see how $group stage works so let's begin.
$group stage is very important stage of aggregation because it is use often in aggregation query. Groups documents by some specified expression and outputs to the next stage a document for each distinct grouping. Let's look in to syntax.

$group Aggregation Stages Syntax

{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }
The _id field is mandatory other are optional, we can specify an _id value of null, or any other constant value. Now let's see how it's works, before group stage we have set of documents, those document has certain fields and if we want to group those document by certain fields name, then we should use these fields name as expression on the right side of _id as key value pair and as a result we will get documents with one fields _id and values which will depend on field we use in expression. It may sound difficult but when we start working with it will get easy let's look in to example.
 db.getCollection("person").aggregate([{$group:{_id:"$age"}}]);
In above example we grouping documents by age fields and finding distinct values of age fields and then producing new set of document and each of new document have just one field name _id. Values will be distinct values of age fields of input documents e.g. shown in below image.

$group Aggregation Stages Example


Lets look in to another example
db.getCollection("person").aggregate([{$group:{_id:{age:"$age",gender:"$gender"}}}]);
Second example we grouping data with two fields age and gender, so we will find unique pair document of age and gender and output as separate document. each output have nested document as values. These nested documents will have two fields age and gender. Below image is output of second query.

$group Aggregation Stages Example

Above was just introduction of $group stage. We will see more in detail and more example and combination of $match and $group stage in next tutorials.

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

$match Aggregation Stages - MongoDB Aggregation Tutorial For Beginner

Leave a Comment
Now let's talk about mongodb Aggregation stages and first stage will $match stage. We already familiar with mongodb query. Match Stages use query as argument and same as find method so its easy to learn.

$match aggregation stages syntax

{ $match: { <query> }}

$match example

{$match:{country:"Poland"}}
{$match:{age:{$gt:"20"}}}
So first example query looking for all document whose city is Poland and second example age greater than 20, all document where field age is greater than 20. Match use standard MongoDB queries and support all query operation. Next we look match query with our database.

db.getCollection("person").aggregate([{$match:{gender:"Male"}}]);
In above query, we get all data whose gender is male you can see in below image.

$match Aggregation Stages

Aggregate $match query is same as mongodb find method with query arguments. Next tutorials we will see $group aggregation stages.

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

Aggregation Stages - MongoDB Aggregation Tutorial for beginner

Leave a Comment
In Last tutorial we have seen basic concept of mongodb aggregation. In this tutorial we gonna take tour of mongodb aggregation stages.

For this whole tutorials series i am gonna use ROBO 3T to execute mongodb query. you can download ROBO 3T software from this website https://robomongo.org/ and download database from this google drive link.

Mongodb Aggregation without any stages

In Previous blog we have seen syntax of mongodb aggregate function so let's execute simple aggregate query on our student collection.
db.getCollection("person").aggregate([]);

Mongodb Aggregation without any stages

In above query we pass blank array as an argument to mongodb aggregate and as a result we get all document of collection it's means without any stages aggregate produce same result like mongodb find method with empty query. let's execute find method of mongodb.
db.getCollection("person").find({});

Mongodb Find method

If you see both query produce same result. Now let's talk about aggregation stages.

Aggregation Stages

Let's discuses more deeper what is aggregation stage? which stage are exist in mongodb aggregation framework. Each stage work independently, each stage take input of document then perform its operation and produce output document. Some stages produce same document as input document like sort and limit stages or it can be produce new document when we use group stages. Let's look stages operator

Aggregation Stages Operators

{ $<stageoperator> : {} }
Each stage start from stage operator append with $ sign then comes object, object contain key value pair let's see and example
{ $match : {score: {$gt:20} } }
{ $sort : { count: -1} }
In above example we can see match and sort stage shown, we will discuses each stage operator individually later in this tutorials. For now you just remember how to construct stage. Below are some mongodb aggregation stages with quick overview.

  • $match: It is use filter document based on certain query, this help to reduce document which are gonna pass to next stage.
  • $group: Group document using certain criteria.
  • $project: Filter fields in documents.
  • $unwind: Unwind takes an array field and separate each array fields in to documents.
  • $sort: Sorts the documents.
  • $count: Count number of objects documents.
  • $limit: Limit number of documents.
  • $skip: Skip certain amount of documents.
  • $out: Out writes aggregation result in to another collections.


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

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 .
Powered by Blogger.