0% found this document useful (0 votes)
2 views4 pages

WK 2 4 MongoDB Aggregation

This document outlines a lab on MongoDB aggregation, detailing objectives such as using aggregation operators ($sort, $limit, $group, etc.) and building aggregation pipelines. It includes exercises for setting up the environment, performing operations like sorting and grouping data, and finding insights from the data. Additionally, it provides practice exercises and information about the authors and change log.

Uploaded by

nguyentluc19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

WK 2 4 MongoDB Aggregation

This document outlines a lab on MongoDB aggregation, detailing objectives such as using aggregation operators ($sort, $limit, $group, etc.) and building aggregation pipelines. It includes exercises for setting up the environment, performing operations like sorting and grouping data, and finding insights from the data. Additionally, it provides practice exercises and information about the authors and change log.

Uploaded by

nguyentluc19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/3/23, 8:35 PM about:blank

MongoDB Aggregation

Estimated time needed: 45 minutes

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!

About Skills Network Cloud IDE


Skills Network Cloud IDE (based on Theia and Docker) provides an environment for hands on labs for course and project related labs. Theia is
an open source IDE (Integrated Development Environment), that can be run on desktop or on the cloud. to complete this lab, we will be using
the Cloud IDE based on Theia and MongoDB running in a Docker container.

Important Notice about this lab environment


Please be aware that sessions for this lab environment are not persisted. Every time you connect to this lab, a new environment is created for
you. Any data you may have saved in the earlier session would get lost. Plan to complete these labs in a single session, to avoid losing your data.

Exercise 1 - Getting the environment ready


1. Problem:

Start the mongodb server.

Click here for Hint


Click here for Solution

2. Problem:

Connect to the mongodb server.

Click here for Hint


Click here for Solution

Load sample data into the training database.

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!

Exercise 2 - Limiting the rows in the output


Using the $limit operator we can limit the number of documents printed in the output.
This command will print only 2 documents from the marks collection.

1. 1
2. 2

1. use training
2. db.marks.aggregate([{"$limit":2}])

Copied!

Exercise 3 - Sorting based on a column


We can use the $sort operator to sort the output.

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!

Exercise 4 - Sorting and limiting


Aggregation usually involves using more than one operator.
A pipeline consists of one or more operators declared inside an array.
The operators are comma separated.
Mongodb executes the first operator in the pipeline and sends its output to the next operator.

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!

The above query is equivalent to the below sql query.

SELECT subject, average(marks)


FROM marks
GROUP BY subject

Exercise 6 - Putting it all together


Now let us put together all the operators we have learnt to answer the question. “Who are the top 2 students by average marks?”
This involves:

finding the average marks per student.


sorting the output based on average marks in descending order.
limiting the output to two documents.
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
15. 15

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.

Click here for Hint


Click here for Solution

2. Problem:

Find the maximum marks scored in each subject.

Click here for Hint


Click here for Solution

3. Problem:

Find the minimum marks scored by each student.

Click here for Hint


Click here for Solution

4. Problem:

Find the top two subjects based on average marks.

Click here for Hint


Click here for Solution

5. Problem:

Disconnect from the mongodb server.

Click here for Hint


Click here for Solution

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

Copyright (c) 2021 IBM Corporation. All rights reserved.

about:blank 4/4

You might also like