MongoDB aggregation
This article will provide step by step advanced and basic concept of MongoDB.where you can easily understand the core concept of MongoDB.MongoDB aggregation function helps analytical in MongoDB that allows summarizing a large amount of data.This function provides analytical on one or more MongoDB document of collection and returns a single value out of entire group.In SQL <count()><AVG()><SUM()> <MIN()>is equal MongoDB aggregation.which show below.
Prerequisite
It would be better if you have the basic knowledge of RDBMS concept. Actually, you are going to learn a high-performance database so we need some basics of these.It will good to cover my below articles before this.Its help to learn more about MongoDB.In which I explained.Click here MongoDB Map- reduce working and Data modeling in MongoDB and SQL difference.
Aggregation Modes
Three types of aggregation modes in MongoDB.
- Aggregation pipeline.
- Map-reduce.
- Single aggregation operation.
Implementation section
- Aggregation Method().
- Aggregation pipeline.
- Map-reduce.
- Single aggregation operation.
- MongoDB aggregation operator
Aggregation Method()
The following is syntax of aggregation method–
1 | db.Name_OF_collection.Arregate(operation_Arregation) |
- <db> is a reserved word.
- <Collection_name> writes the collection name.
- <Arregate> is also reserved word used for computing single result.
- Inside the bracket write the document <specific_criteria for Arregation>
Suppose we have following data–
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | { "isActive": false, "age": 29, "name": "programmaerhelper new site for beginners", "gender": "female", "amount": "250, "email": "programmaershelper@gmail.com", "phone": "+1 (928) 524-3812", "address": "Heloword" }, { "isActive": false, "age": 30, "name": "programmaerhelper Help center", "gender": "female", "amount": "450, "email": "programmaershelper@gmail.com", "phone": "+1 (928) 524", "address": "Heloword" } { "isActive" : true, "balance" : "$1,921.51", "age" : 24, "eyeColor" : "green", "name" : "ddd", "gender" : "male", "amount": "1030,, "email" : "programmmershelper@.com", "phone" : "+1 (976) 490-3620", "address" : "FSD" } |
Aggregation pipeline
In Aggregation, pipelines queries apply on a group and filtered result.it provides internal optimizing.
The following syntax is aggregation pipelines–
1 | db.programmershelpers.aggregate([{$group:{_id:"$age",total:{$sum:"$amount"}}}]) |
Output:
Map-reduce
For Map reduce want to follow this link Map-reduce working in MongoDB step by step guideline.
Single aggregation operation
This function gives a single value depends on condition.All of these aggregation operations are apply document of a single collection.while single aggregation operation giving the simple result as compared aggregation pipeline and Map-reduce.
The following is basic syntax–
1 | db.programmershelpers.distinct("inside this spcific value") |
We used above document and apply queries in three different ways.
Output:
MongoDB aggregation operator
They are different types of aggregation operator use in MongoDB.The aggregation operator summarising larger amount of data and provides limited result out of the whole.The following are aggregation operator.
- $project – Select some field in the collection.
- $limit – Showing limit number of the document.
- $match – filter data
- $group – aggregate data
- $sort – sorts document
- $skip – skips document
- $unwind – normalizes data
$project
Select some field in the collection.For more detail click here MongoDB projection working.
$limit and $skip
$limit Showing limit number of the document.$skip -skips document.For more detail click here MongoDB skip and limit step by step working.
$match
Th $match only gives match data in the collection.$match is similar to where in SQL.i just match <status> <programmerhelpersMongoDB> in collection.its gives result how many time it reacted.
1 2 3 4 | db.programmershelper.aggregate( { $match : {status : "ProrammershelpersMongoDB"} } |
Output
$sort
Sorts document.For more detail click here MongoDB Sort document.
Conclusion
I wish I could tell you that a great site of MongoDB.you just understands key element above post-MongoDB aggregation.Click here More detail about aggregation. I hope you will understand this lecture.Thank you for reading this lecture. Hope you got the idea. please share it.