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

9 comments
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

3 comments
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

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