0% found this document useful (0 votes)
2 views

Program5_WM

The document outlines a program for executing aggregation operations in MongoDB, specifically focusing on the Academics database and students collection. It includes instructions for adding student documents and performing various aggregation queries such as calculating average grades, finding minimum ages, and listing unique courses. The document provides example MongoDB commands for each operation, demonstrating the use of aggregation operators like $avg, $min, $max, $push, and $addToSet.

Uploaded by

nainitharao.b
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 views

Program5_WM

The document outlines a program for executing aggregation operations in MongoDB, specifically focusing on the Academics database and students collection. It includes instructions for adding student documents and performing various aggregation queries such as calculating average grades, finding minimum ages, and listing unique courses. The document provides example MongoDB commands for each operation, demonstrating the use of aggregation operators like $avg, $min, $max, $push, and $addToSet.

Uploaded by

nainitharao.b
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/ 12

Program 5

Execute Aggregation operations ($avg, $min, $max, $push, $addToSet etc.).


Encourage students to execute several queries to demonstrate various
aggregation operators.

Create a database Academics and collection students in Mongo IDE.

Add the following documents in the students collection in MongoDB IDE.

"name": "Alice",

"age": 22,

"grade": 88,

"courses": ["Math", "Physics"]

}
{

"name": "Bob",

"age": 25,

"grade": 92,

"courses": ["Math", "Chemistry"]

}
{

"name": "Charlie",

"age": 23,

"grade": 79,

"courses": ["Biology", "Physics"]

}
{

"name": "David",

"age": 22,

"grade": 95,

"courses": ["Chemistry", "Biology"]

}
{

"name": "Eve",

"age": 25,

"grade": 85,

"courses": ["Math", "Biology"]

}
In MongoShell

>use Academics

1. $avg - Calculate the average grade of all students


> db.students.aggregate([
{
$group: {
_id: null,
averageGrade: { $avg: "$grade" }
}
}
])
Output:

2. $min - Find the minimum age of students


>db.students.aggregate([
{
$group: {
_id: null,
minAge: { $min: "$age" }
}
}
])

Output:

3. $max - Find the maximum grade among students


>db.students.aggregate([
{
$group: {
_id: null,
maxGrade: { $max: "$grade" }
}
}
])

Output:

4. $push - List all student names in an array


db.students.aggregate([
{
$group: {
_id: null,
allNames: { $push: "$name" }
}
}
])

Output:
5. $addToSet - List all unique courses taken by students

>db.students.aggregate([

$unwind: "$courses"

},

$group: {

_id: null,

uniqueCourses: { $addToSet: "$courses" }

])

Output:

You might also like