Examples
By viewing the below example,the concept of MongoDB Aggregation operations can be easily understand.
[c]
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"}
[/c]
Here in the above example the concept of MongoDB Aggregation operations are defined such as maximum and minimum functional operations.