Introduction
In this lab, you will learn how to perform basic aggregation operations in MongoDB. These operations allow you to process data records and return computed results. You will learn to calculate totals, find minimum and maximum values, count documents, and compute averages. The lab uses a sample bookstore collection to demonstrate the power of MongoDB's aggregation framework for extracting meaningful insights from your data.
Connecting and Populating Data
Before we can summarize data, we need to connect to the MongoDB server and insert some sample data. In this step, you will start the MongoDB Shell, create a database, and populate a collection with book information.
First, open your terminal and start the MongoDB Shell (mongosh):
mongosh
You are now inside the MongoDB Shell, which allows you to interact with your databases. Let's switch to a new database called bookstore. If the database does not exist, MongoDB will create it for you when you first store data.
use bookstore
Next, we will insert multiple book documents into a collection named books using the insertMany() method. Each document is a JSON object containing a title, price, and quantity.
db.books.insertMany([
{ title: "Python Basics", price: 29.99, quantity: 50 },
{ title: "MongoDB Essentials", price: 39.99, quantity: 30 },
{ title: "Data Science Guide", price: 45.5, quantity: 25 },
{ title: "Web Development", price: 34.75, quantity: 40 }
]);
You should see an output confirming that the documents were inserted successfully.
To verify that the data has been added, you can use the find() method to retrieve and display all documents in the books collection.
db.books.find();
The output will list the four documents you just inserted, each with a unique _id assigned by MongoDB.
[
{
_id: ObjectId("..."),
title: 'Python Basics',
price: 29.99,
quantity: 50
},
{
_id: ObjectId("..."),
title: 'MongoDB Essentials',
price: 39.99,
quantity: 30
},
{
_id: ObjectId("..."),
title: 'Data Science Guide',
price: 45.5,
quantity: 25
},
{
_id: ObjectId("..."),
title: 'Web Development',
price: 34.75,
quantity: 40
}
]
Now that our collection is set up, we can proceed with performing aggregation operations.
Calculate Totals
In this step, you will learn how to calculate total values using MongoDB's aggregation framework. Aggregation pipelines process data through a series of stages. We will use the $group stage to group documents and the $sum operator to perform calculations.
Let's start by calculating the total number of books in stock across all titles. We will group all documents into a single group and sum their quantity fields.
db.books.aggregate([
{
$group: {
_id: null,
totalQuantity: { $sum: "$quantity" }
}
}
]);
Here is a breakdown of the command:
db.books.aggregate([...]): Initiates an aggregation pipeline on thebookscollection.$group: The stage that groups input documents._id: null: Specifies that all documents in the collection should be grouped together into a single output document.totalQuantity: { $sum: "$quantity" }: Defines a new fieldtotalQuantity. The$sumoperator calculates the sum of thequantityfield (prefixed with$) for all documents in the group.
The expected output is:
[{ "_id": null, "totalQuantity": 145 }]
Next, let's calculate the total inventory value for all books combined. This requires multiplying the price by the quantity for each book and then summing the results.
db.books.aggregate([
{
$group: {
_id: null,
totalBookValue: { $sum: { $multiply: ["$price", "$quantity"] } }
}
}
]);
In this pipeline:
$multiply: ["$price", "$quantity"]: This expression multiplies thepriceandquantityfields for each document.$sum: This operator then adds up the results of the multiplication for all documents.
The expected output is:
[{ "_id": null, "totalBookValue": 5226.7 }]
Find Minimum and Maximum Values
Finding the minimum and maximum values in a dataset is a common analysis task. In this step, you will use the $min and $max aggregation operators to find the cheapest and most expensive books in our collection.
Like $sum, the $min and $max operators are used within the $group stage. Let's create a single aggregation pipeline to find both the lowest and highest book prices.
db.books.aggregate([
{
$group: {
_id: null,
lowestPrice: { $min: "$price" },
highestPrice: { $max: "$price" }
}
}
]);
This pipeline works as follows:
_id: null: Groups all documents together.lowestPrice: { $min: "$price" }: Creates a fieldlowestPriceand sets its value to the minimum value of thepricefield across all documents.highestPrice: { $max: "$price" }: Creates a fieldhighestPriceand sets its value to the maximum value of thepricefield.
The expected output will show the minimum and maximum prices found in the collection:
[{ "_id": null, "lowestPrice": 29.99, "highestPrice": 45.5 }]
You can apply the same logic to other numerical fields. For example, to find the minimum and maximum stock quantities, you would use $min and $max on the quantity field.
db.books.aggregate([
{
$group: {
_id: null,
lowestQuantity: { $min: "$quantity" },
highestQuantity: { $max: "$quantity" }
}
}
]);
The expected output for this query is:
[{ "_id": null, "lowestQuantity": 25, "highestQuantity": 50 }]
Count Documents and Calculate Averages
In this step, you will learn two more useful summarization techniques: counting documents and calculating averages. We will explore both a simple counting method and the aggregation framework for more complex scenarios.
First, let's count the total number of documents in our books collection. The countDocuments() method is the simplest way to do this.
db.books.countDocuments();
The output will be a single number:
4
You can also provide a query filter to countDocuments() to count only the documents that match specific criteria. For example, let's count how many books have a price greater than $35.
db.books.countDocuments({ price: { $gt: 35 } });
The $gt operator stands for "greater than". The output will be:
2
Next, let's calculate the average book price using the aggregation framework. The $avg operator, used within a $group stage, computes the average of a numeric field.
db.books.aggregate([
{
$group: {
_id: null,
averagePrice: { $avg: "$price" }
}
}
]);
This pipeline groups all documents and calculates the average of the price field. The output will be:
[{ "_id": null, "averagePrice": 37.56 }]
The aggregation framework provides a powerful and flexible way to perform calculations like counting and averaging, especially when combined with other stages to handle more complex logic.
Generate a Summary Report
In this final step, you will combine several aggregation stages to generate a more complex summary report. This demonstrates how you can chain operators together to create insightful views of your data. We will create a report that categorizes books into "Budget" and "Premium" price tiers and calculates summary statistics for each.
Our goal is to group books based on their price, then calculate the number of titles, total quantity, total value, and average price for each group.
Run the following aggregation pipeline:
db.books.aggregate([
{
$group: {
_id: {
$cond: {
if: { $lt: ["$price", 35] },
then: "Budget",
else: "Premium"
}
},
totalBooks: { $sum: 1 },
totalQuantity: { $sum: "$quantity" },
totalValue: { $sum: { $multiply: ["$price", "$quantity"] } },
avgPrice: { $avg: "$price" }
}
}
]);
Let's break down this powerful pipeline:
$group: This stage is the core of our report._id: { $cond: ... }: Instead ofnull, we use the$cond(conditional) operator to dynamically determine the group ID. If a book'spriceis less than ($lt) 35, it's assigned to the "Budget" group; otherwise, it's "Premium".totalBooks: { $sum: 1 }: This is a common technique for counting documents within a group. For each document, it adds1to the total.totalQuantity,totalValue,avgPrice: These are the same accumulators you learned in previous steps, but now they operate within each price category.
The output provides a clean summary for each category. Note that the order of the documents may vary.
[
{
"_id": "Premium",
"totalBooks": 2,
"totalQuantity": 55,
"totalValue": 2212.75,
"avgPrice": 42.745
},
{
"_id": "Budget",
"totalBooks": 2,
"totalQuantity": 90,
"totalValue": 2984.5,
"avgPrice": 32.37
}
]
This example shows how the aggregation framework can be used to transform raw data into structured, meaningful reports directly within the database.
Summary
In this lab, you have learned the fundamentals of data aggregation in MongoDB. You started by connecting to a database and populating a collection with sample data. You then used the aggregation framework to perform several common summarization tasks. You have learned how to use the $group stage with operators like $sum to calculate totals, $min and $max to find boundary values, and $avg to compute averages. You also practiced using countDocuments() for simple counts and saw how to build a multi-faceted summary report by combining multiple operators and stages. These skills are essential for analyzing data and deriving valuable insights from your MongoDB collections.

