0% found this document useful (0 votes)
6 views12 pages

Aggregation Nates

The document provides a sample dataset of student scores and outlines various aggregation exercises using MongoDB. It includes examples for counting students, calculating average scores, finding maximum scores, and summing scores, all grouped by student names. Additionally, it explains the purpose and structure of the aggregation pipeline, demonstrating how to manipulate and analyze data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Aggregation Nates

The document provides a sample dataset of student scores and outlines various aggregation exercises using MongoDB. It includes examples for counting students, calculating average scores, finding maximum scores, and summing scores, all grouped by student names. Additionally, it explains the purpose and structure of the aggregation pipeline, demonstrating how to manipulate and analyze data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SAMPLE DATASET

_id: 1,

name: "Alice",

scores: [

{ score: 85, type: "exam" },

{ score: 92, type: "quiz" },

{ score: 78, type: "homework" }

},

_id: 2,

name: "Bob",

scores: [

{ score: 79, type: "exam" },

{ score: 95, type: "quiz" },

{ score: 88, type: "homework" }

},

_id: 3,

name: "Charlie",

scores: [

{ score: 91, type: "exam" },

{ score: 82, type: "quiz" },

{ score: 85, type: "homework" }

]
Exercises:

1. Count the Number of Students:

Exercise: Group by name and count the number of Students.

Aggregation:

db.Student_database.aggregate([

$group: {

_id: "$name",

count: { $sum: 1 }

])

2. Calculate the Average Score of Each Student:

Exercise: Group by name and calculate the average score.

db.Student_database.aggregate([

$group: {

_id: "$name",

avgScore: { $avg: { $avg: "$scores.score" } }

])
3. Find the Maximum Score of Each Student:

 Exercise: Group by name and find the maximum score.

db.Student_database.aggregate([

$group: {

_id: "$name",

maxScore: { $max: { $max: "$scores.score" } }

])

4. Sum Scores by Student:

 Exercise: Group by name and calculate the total score.

db.Student_database.aggregate([

$group: {

_id: "$name",

totalScore: { $sum: { $sum: "$scores.score" } }

])
Purpose of the Aggregation

The purpose of this aggregation pipeline is to group documents in the collection by the `name` field
and count the number of documents for each unique `name`.

Aggregation Pipeline

db.collection.aggregate([

$group: {

_id: "$name",

count: { $sum: 1 }

])

1. `db.collection.aggregate([...])`

- This function runs an aggregation operation on the collection named `collection`.

2. `$group` Stage

- The `$group` stage is used to group documents by a specified key and perform aggregation
operations on the grouped data.

3. `_id: "$name"`

- The `_id` field in the `$group` stage is used to specify the key by which to group the documents.

- Here, `"$name"` means that documents will be grouped by the value of the `name` field.

- Each unique `name` will be a separate group.

4. `count: { $sum: 1 }`

- This creates a new field called `count` in the resulting documents.


- `{ $sum: 1 }` is an aggregation operator that sums the specified expression. In this case, the
expression is `1`.

- For each document in a group, `1` will be added to the sum, effectively counting the number of
documents in each group.

Result

The result of this aggregation will be a document for each unique `name`, with a field `count` that
shows the number of documents with that `name`.

Example

Consider the following sample documents in the collection:

json DataSet

{ _id: 1, name: "Alice", age: 25 },

{ _id: 2, name: "Bob", age: 30 },

{ _id: 3, name: "Alice", age: 28 },

{ _id: 4, name: "Charlie", age: 35 },

{ _id: 5, name: "Bob", age: 32 }

After running the aggregation pipeline, the result will be:

```json

{ _id: "Alice", count: 2 },

{ _id: "Bob", count: 2 },

{ _id: "Charlie", count: 1 }

```
Summary

- Grouping: The documents are grouped by the `name` field.

- Counting: The `count` field indicates how many documents there are for each `name`.

- Output: Each document in the result represents a unique `name` and the count of occurrences of
that `name` in the original collection.

EXAMPLE -2
Example 1: Counting Documents by a Specific Field

Goal: Count the number of documents for each unique value in the `category` field.

Sample Data:

```json

{ _id: 1, category: "Fruit", name: "Apple" },

{ _id: 2, category: "Fruit", name: "Banana" },

{ _id: 3, category: "Vegetable", name: "Carrot" },

{ _id: 4, category: "Fruit", name: "Orange" },

{ _id: 5, category: "Vegetable", name: "Broccoli" }

```

Aggregation Pipeline:

```json

db.collection.aggregate([

$group: {

_id: "$category",

count: { $sum: 1 }

}
}

])

```

Explanation:

- Group documents by the `category` field.

- Count the number of documents in each category by summing `1` for each document.

Result:

```json

{ _id: "Fruit", count: 3 },

{ _id: "Vegetable", count: 2 }

```

Example 2: Summing Values in a Field

Goal: Calculate the total sales for each product.

Sample Data:

```json

{ _id: 1, product: "Apple", sales: 150 },

{ _id: 2, product: "Banana", sales: 200 },

{ _id: 3, product: "Apple", sales: 100 },

{ _id: 4, product: "Banana", sales: 150 },

{ _id: 5, product: "Orange", sales: 250 }

```
Aggregation Pipeline:

```json

db.collection.aggregate([

$group: {

_id: "$product",

totalSales: { $sum: "$sales" }

])

```

Explanation:

- Group documents by the `product` field.

- Sum the `sales` field for each product.

Result:

```json

{ _id: "Apple", totalSales: 250 },

{ _id: "Banana", totalSales: 350 },

{ _id: "Orange", totalSales: 250 }

```

Example 3: Calculating the Average

Goal: Calculate the average score for each student.


Sample Data:

```json

{ _id: 1, student: "Alice", score: 85 },

{ _id: 2, student: "Bob", score: 90 },

{ _id: 3, student: "Alice", score: 95 },

{ _id: 4, student: "Bob", score: 80 }

```

Aggregation Pipeline:

```json

db.collection.aggregate([

$group: {

_id: "$student",

averageScore: { $avg: "$score" }

])

```

Explanation:

- Group documents by the `student` field.

- Calculate the average `score` for each student.

Result:

```json

{ _id: "Alice", averageScore: 90 },

{ _id: "Bob", averageScore: 85 }


]

```

Example 4: Finding the Maximum Value

Goal: Find the maximum score for each subject.

Sample Data:

```json

{ _id: 1, subject: "Math", score: 85 },

{ _id: 2, subject: "Science", score: 90 },

{ _id: 3, subject: "Math", score: 95 },

{ _id: 4, subject: "Science", score: 88 }

```

Aggregation Pipeline:

```json

db.collection.aggregate([

$group: {

_id: "$subject",

maxScore: { $max: "$score" }

])

```
Explanation:

- Group documents by the `subject` field.

- Find the maximum `score` for each subject.

Result:

```json

{ _id: "Math", maxScore: 95 },

{ _id: "Science", maxScore: 90 }

```

Example 5: Collecting Values into an Array

Goal: Collect all scores for each student into an array.

Sample Data:

```json

{ _id: 1, student: "Alice", score: 85 },

{ _id: 2, student: "Bob", score: 90 },

{ _id: 3, student: "Alice", score: 95 },

{ _id: 4, student: "Bob", score: 80 }

```
Aggregation Pipeline:

```json

db.collection.aggregate([

$group: {

_id: "$student",

scores: { $push: "$score" }

])

```

Explanation:

- Group documents by the `student` field.

- Collect all `score` values into an array for each student.

Result:

```json

{ _id: "Alice", scores: [85, 95] },

{ _id: "Bob", scores: [90, 80] }

```

You might also like