MongoDB - SPLessons

MongoDB Aggregation

Chapter 18

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Aggregation

MongoDB Aggregation

shape Description

MongoDB Aggregation functions execute informational records and rebounds the expected output. In MongoDB Aggregation operations are used where as in sql database count functions are performed. The functions of aggregation are an association of key esteems among different records that are well balanced, and execute an assortment of activities that associates the information to give back a solitary output.

shape Syntax

The syntax for MongoDB Aggregation functions is as follows:
>db.collection_name.aggregate(aggregate_operation)

Aggregate operation =>Process the required records.

Aggregate functional commands

shape Description

The following are the aggregate operation in mongodb database.

Function Explanation Example
$sum sum up the characterised esteem from all the records from the set. db.mycol.aggregate([{$group:{_id:”$by_user”,num_tutorials:{$sum:”$likes”}}}])
$min Retrieves the minimum esteem in the corresponding records. db.mycol.aggregate([{$group:{_id:”$by_user”,num_tutorial:{$min:”$like”}}}])
$avg Ascertains the normal of every given quality from all reports in the gathering. db.mycol.aggregate([{$group:{_id:”$by_user”,num_tutorial:{$avg:”$like”}}}])
$max Retrieves the maximum esteem in the corresponding records. db.mycol.aggregate([{$group:{_id:”$by_user”,num_tutorial:{$max:”$likes”}}}])
$last Retrives the last record from the main record set db.mycol.aggregate([{$group:{_id:”$by_user”,last_url:{$last:”$url”}}}])
$first Retrives the first record from the corresponding record set db.mycol.aggregate([{$group:{_id:”$by_user”,first_url:{$first:”$url”}}}])
$addToset Inserting the esteems inside the array db.mycol.aggregate([{$group:{_id:”$by_user”,first_url:{$first:”$url”}}}])
$push Inserting the esteems inside the array from the output records db.mycol.aggregate([{$group:{_id:”$by_user”,url:{$addToset:”$url”}}}])

shape Examples

By viewing the below example,the concept of MongoDB Aggregation operations can be easily understand.

MongoDB shell version:2.6.1
connecting to: test
use school
switched to db school
>db.students.find();
{"_id":objectId("56c63217089b3c7e483bd97"),"StudentNo": "1","FirstName":"Mark","LastName":"Masen","Gender":"Male","Age":"14"}
{"_id":objectId("56c63217089b3c7e483bd98"),"StudentNo": "2","FirstName":"Mel","LastName":"Gibson","Gender":"Female","Age":"17"}
{"_id":objectId("56c63217089b3c7e483bd99"),"StudentNo": "3","FirstName":"Deno","LastName":"Socratus","Gender":"Male","Age":"40"}
{"_id":objectId("56c63217089b3c7e483bd9a"),"StudentNo": "4","FirstName":"Shay","LastName":"Whatson","Gender":"Female","Age":"32"}
>db.students.aggregate([{$group:{_id:"$Gender",MyResult:{$sum:1}}}]);
{"_id":"Male","MyResult":3}
{"_id":"Female","MyResult":3}
>db.students.aggregate([{$group:{_id:"$Gender",MaxAge:{$max:"Age"}}}]);
{"_id":"Male","MaxAge":"Age"}
{"_id":"Female","MaxAge":"Age"}
>db.students.aggregate([{$group:{_id:"$Gender",MaxAge:{$max:"Age"}}}]);
{"_id":"Male","MaxAge":"40"}
{"_id":"Female","MaxAge":"32"}
>db.students.aggregate([{$group:{_id:"$Gender",MinAge:{$min:"Age"}}}]);
{"_id":"Male","MinAge":"14"}
{"_id":"Female","MinAge":"17"}

Here in the above example the concept of MongoDB Aggregation operations are defined such as maximum and minimum functional operations.

Summary

shape Key Points

  • MongoDB Aggregations – Are the functions that execute informational records and rebounds the desire output.
  • Aggregate functions- Aggregate functions are $sum, $min, $max, $avg, $last, $first, $addToset, $push.