Working with Aggregation in MongoDB

In this article we will be discussing about aggregation in mongodb.

What is aggregation in mongodb?

As it states in mongodb.org
“Aggregations are operations that process data records and return computed results.”

So, in simple terms aggregation uses multiple documents together to group values. With those values aggregration can perform multiple operations and return only one result. We can compare this feature with group by in sql count(*).Like queries, aggregation operations in mongodb use collections of documents as an input and return results in the form of one or more documents.

How do we use aggregation in mongodb?

To invoke aggregation we need to use aggregate() method. Let us get a view of a basic syntax for better understanding:


db.collection.aggregate(aggregate_operation)

Aggregation expressions:

In the following we are going to discuss about a few of the expressions that mongodb provides.

$sum:

This expression is for summing up the defined value from all documents in the collection. Let us see an example:


db.collection_name.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])

$avg:
This expression calculates the average of all given values from all documents in the collection.Let us see an example:


db.collection_name.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])

$first:
This expression is for getting the first document from the source documents according by the grouping. This makes only sense together with some previously applied “$sort”-stage. Let us see an example:


db.collection_name.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])

$last:
This expression is used for getting the last document from the source documents according by the grouping. This makes only sense together with some previously applied “$sort”-stage. Let us see an example:


db.collection_name.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])

$max:
This expression is for getting the maximum of the corresponding values from all of the documents in the collection.Let us see an example:


db.collection_name.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])

$min:
This expression is for getting the minimum of the corresponding values from all of the documents in the collection.Let us see an example:


db.collection_name.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])

$push:
This expression is for inserting the value to an array in the resulting document. Let us see an example:


db.collection_name.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])

$size:
This expression is used to count and return the total the number of items in an array.Let us see an example:


{<field>: { $size:<array>  } }

$map:
$map applies a sub-expression to each item in an array and returns an array with the result of the sub-expression. $map is available in the $project, $group, and $redact pipeline stages.Let us see an example:

In this example we will work with a lot of operations of addition with different values. And we will also try to show the result in a single array.
At first we need to provide data as follows:


{ test: [ 1, 1, 2, 3, 5, 8 ] }

Next let us continue with the $project statement:


{ $project: { adjustments: { $map: { input: "$test",
                                     as: "adj",
                                     in: { $add: [ "$$adj", 12 ] } } } } }

The $map would add 12 with every data as provided. And thus the result is:


{ adjustments: [ 13, 13, 14, 15, 17, 20 ] }

$let:
This expression is used to bind variables for use in sub-expressions. To access the variable, use a string with the variable name prefixed with double dollar signs ($$). Returns the value of the sub-expression evaluated with the bound variables. Let us understand through a example:


{
  $let:
     {
       vars: { : , ... },
       in: {  }
     }
}

$literal:
This expression is used for wrapping up an expression to prevent the aggregation pipeline from evaluating the expression. Let us understand through a basic example:
In this example we are going to see how can we give another string to evaluate for $.


db.collection name.aggregate( [
   { $project: { costsOneDollar: { $eq: [ "$price", { $literal: "$1" } ] } } }
] )

In the above example we are giving another string to the $ sign to differentiate with the expression.

There are many operators that we can use in the aggregate() method.  Although this article was all about aggregation . There is another aspect of this feature. We will discuss about that in the upcoming articles.

Related Links:

1> How to Get Started with MongoDB Database?
2> How to Get Started with MongoDB?
3> How to Import and Export Through Mongodb?
4> How to Use Projection in MongoDB?
5> Using sort method in mongodb
6> Map-Reduce in MongoDB
7> Introduction to Replication in MongoDB
8> Deploying a Replica Set in MongoDB
9> Discussing Replication Lag in MongoDB
10> Replica Set Members in Mongodb
11> Working with Sharding in MongoDB
12> Working with Index in MongoDB
13> How to Work with Aggregation Framework in MongoDB?
14> Working with Pipeline Concept in MongoDB
15> Discussing about Pipeline Expression in MongoDB

If you find this article helpful, you can connect us in Google+ and Twitter.

Leave a Reply

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