Mongo DB Aggregation

In this Article, we will learn how to get particular document list based on the condition, grouping of data, how to select particular fields from the document as output. All this operation will be preformed by Mongo DB Aggregation.

Aggregation syntax is:

db.collectionName.aggregate([
    stage 1,
    stage 2,
    ...,
    ...,
    stage N
])

Here, Stage means filtering. we perform query on data in stage 1 and pass the resultant data to stage 2. It will perform further filtration and pass it to subsequent stage from the aggregate pipeline.

Aggregate stages are $match, $group, $project, $sort, $count, $limit, $skip and so on. In this article, we will learn some of the aggregate stage operation from above list.

Stage syntax is:

{ $<stageOperator>: { } }

Example:

{ $match: { age: { $gt:20 } } }
{ $group: { _id: "age" } }
{ $sort: { count: -1 } }

Let’s learn some of the aggregate stages in this article.

Match:

This stage get the list of documents as input perform a match operation on it and return selected documents as output. This operator is equivalent to where condition in SQL.

Example:

{ $match: { <query> } }
{ $match: { city: "New York" } }
{ $match: { age: { $gt: 25 } } }

Here, First query return the documents having city equals to New York. While second query return the list of document having age is greater than 25.

We can also use logical operator in match stage like.

{ $match: { $and: [ { gender: "Female" }, { age: { $gt: 25 } } ] } }

The above query return the list of document having gender as a Female and her age is greater than 25.
The whole query will be like this:

db.persons.aggregate([
    { $match: { active: true } }
])

Group:

This stage accept a group of document and perform a group operation on a specific _id fields and return a resultant output.

Example:

{ $group: { _id: "$age" } }
{ $group: { _id: { age: "$age", gender: "$gender" } } }

Here, First query return a list of document group by age. While second query return a list of document by age and gender.
We can write Group with match stage like:

db.persons.aggregate([
    { $match: { city: "New York" } },
    { $group: { _id: "age" } }
])

$match and $group stage are equivalent to SQL query like:

  1. $match -> $group is equivalent to Where -> Group
  2. $group -> $match is equivalent to Group -> Having

Count:

This stage take a list of documents and return a number of document. This is equivalent to count(*) in SQL.

Example:

{ $count: "personCount" }

This will return a personCount as key and value will be a number of person in the list.

There is 2 other method also to calculate a count of the collection.

Method 1:

db.persons.aggregate([]).itcount()

This method will return all document then count the number of document in a collection. So this methos take long time as compare to $count stage.

Method 2:

db.persons.aggregate([]).toArray.length

This method will return list of collection, covert it to array then return the length of that array. Those method also take long time as compared to $count stage.

Whole query we can write as follow:

db.persons.aggregate([
    { $match: { city: "New York" } }
    { $count: "NY" }
])

Output will be:

{ NY: 5000 }

Project:

This stage is used to return a selected fields from the document. Syntax will be

{ $project: { <field1>: <1|0>, <field2>: <1|0> } }

Whatever the field you want to retrieve use 1 in front of them and which you don’t want to retrieve as a result use 0 in front of them.

Example:

{ $project: { name: 1 } }

Above query only return a name and _id filed from the person collection. If you want to hide _id field from the output you can rewrite above query as

{ $project: { name: 1, _id: 0 } }

 

Thank you guys for reading this blog and I hope you found something useful. Please give your valuable feedback/comments/questions about this article. Please let me know how you like and understand this article and how I could improve it.

 

 

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories