WK 2 4 MongoDB Aggregation
WK 2 4 MongoDB Aggregation
MongoDB Aggregation
Objectives
After completing this lab you will be able to:
Describe simple aggregation operators that process and compute data such as $sort, $limit, $group, $sum, $min, $max, and $avg
Combine operators to create multi-stage aggregation pipelines
Build aggregation pipelines that draw insights about the data by returning aggregated values
Prerequisites
Before starting this lab, it’ll be helpful to have knowledge about accessing and performing basic operations with MongoDB. If you’re unfamiliar
with MongoDB, feel free to take a look at the Getting Started with MongoDB and MongoDB CRUD labs!
2. Problem:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
about:blank 1/4
10/3/23, 8:35 PM about:blank
1. use training
2. db.marks.insert({"name":"Ramesh","subject":"maths","marks":87})
3. db.marks.insert({"name":"Ramesh","subject":"english","marks":59})
4. db.marks.insert({"name":"Ramesh","subject":"science","marks":77})
5. db.marks.insert({"name":"Rav","subject":"maths","marks":62})
6. db.marks.insert({"name":"Rav","subject":"english","marks":83})
7. db.marks.insert({"name":"Rav","subject":"science","marks":71})
8. db.marks.insert({"name":"Alison","subject":"maths","marks":84})
9. db.marks.insert({"name":"Alison","subject":"english","marks":82})
10. db.marks.insert({"name":"Alison","subject":"science","marks":86})
11. db.marks.insert({"name":"Steve","subject":"maths","marks":81})
12. db.marks.insert({"name":"Steve","subject":"english","marks":89})
13. db.marks.insert({"name":"Steve","subject":"science","marks":77})
14. db.marks.insert({"name":"Jan","subject":"english","marks":0,"reason":"absent"})
Copied!
1. 1
2. 2
1. use training
2. db.marks.aggregate([{"$limit":2}])
Copied!
This command sorts the documents based on field marks in ascending order.
1. 1
2. 2
1.
2. db.marks.aggregate([{"$sort":{"marks":1}}])
Copied!
This command sort the documents based on field marks in descending order.
1. 1
2. 2
1.
2. db.marks.aggregate([{"$sort":{"marks":-1}}])
Copied!
Let us create a two stage pipeline that answers the question “What are the top 2 marks?”.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
about:blank 2/4
10/3/23, 8:35 PM about:blank
11. 11
12. 12
13. 13
14. 14
15. 15
16. 16
17. 17
18. 18
19. 19
20. 20
21. 21
22. 22
1. db.marks.aggregate([
2. {"$sort":{"marks":-1}},
3. {"$limit":2}
4. ])
5. ```
6.
7. # Exercise 5 - Group by
8.
9. The operator $group by, along with operators like $sum, $avg, $min, $max, allows us to perform grouping operations.
10.
11. This aggregation pipeline prints the average marks across all subjects.
12.
13. ```
14.
15. db.marks.aggregate([
16. {
17. "$group":{
18. "_id":"$subject",
19. "average":{"$avg":"$marks"}
20. }
21. }
22. ])
Copied!
1.
2. db.marks.aggregate([
3. {
4. "$group":{
5. "_id":"$name",
6. "average":{"$avg":"$marks"}
7. }
8. },
9. {
10. "$sort":{"average":-1}
about:blank 3/4
10/3/23, 8:35 PM about:blank
11. },
12. {
13. "$limit":2
14. }
15. ])
Copied!
Practice exercises
1. Problem:
Find the total marks for each student across all subjects.
2. Problem:
3. Problem:
4. Problem:
5. Problem:
Authors
Ramesh Sannareddy
Other Contributors
Rav Ahuja
Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
2021-11-17 0.3 Kathy An Updated lab instructions
2021-04-19 0.2 Steve Ryan Review pass
2021-03-24 0.1 Ramesh Sannareddy Created initial version of the lab
about:blank 4/4