Lesson 01: Introduction to MongoDB Aggregation
Lesson 02: Using $match and $group Stages in a MongoDB Aggregation Pipeline
Lesson 03: Using $sort and $limit Stages in a MongoDB Aggregation Pipeline
Lesson 04: Using $project, $count, and $set Stages in a MongoDB Aggregation Pipeline
Lesson 05: Using $out Stage in a MongoDB Aggregation Pipeline
This section contains key definitions for this lesson, as well as the code for an aggregation pipeline.
Aggregation: Collection and summary of data
Stage: One of the built-in methods that can be completed on the data, but does not permanently alter it
Aggregation pipeline: A series of stages completed on the data in order
db.collection.aggregate([
{
$stage1: {
{ expression1 },
{ expression2 }...
},
$stage2: {
{ expression1 }...
}
}
])
Review the following sections, which show the code for the $match and $group aggregation stages.
The $match stage filters for documents that match specified conditions. Here's the code for $match
:
{
$match: {
"field_name": "value"
}
}
The $group stage groups documents by a group key.
{
$group:
{
_id: <expression>, // Group key
<field>: { <accumulator> : <expression> }
}
}
The following aggregation pipeline finds the documents with a field named "state" that matches a value "CA" and then groups those documents by the group key "$city" and shows the total number of zip codes in the state of California.
db.zips.aggregate([
{
$match: {
state: "CA"
}
},
{
$group: {
_id: "$city",
totalZips: { $count : { } }
}
}
])
You are now connected to an Atlas cluster and to the bird_data database. Use the sightings collection in this lab.
First, create an aggregation pipeline, which will contain two stages. (Forgot the method for aggregation? Check the hint below!)
Create a $match stage that filters for documents about our target bird with the species_common value, "Eastern Bluebird"
Create a $group stage that groups documents based on the latitude and longitude of the sighting, stored in the location.coordinates field.
Within the groups, create a field to show the $count of how many documents are in each group, called "number_of_sightings".
Run your aggregation pipeline, and find out where to look for Eastern Bluebirds!
Once you complete this lab, compare your answer to the correct answer in the Review and Solved Code section, then select Next.
Once you complete this lab, compare your answer to the correct answer in the Review and Solved Code section, then select Next
db.sightings.aggregate([
{
$match:{species_common:"Eastern Bluebird"}
},
{
$group: {
_id: "$location.coordinates",
number_of_sightings: { $count : { } }
}
}
])
Review the following sections, which show the code for the $sort and $limit aggregation stages.
The $sort stage sorts all input documents and returns them to the pipeline in sorted order. We use 1 to represent ascending order, and -1 to represent descending order.
{
$sort: {
"field_name": 1
}
}
The $limit stage returns only a specified number of records.
{
$limit: 5
}
The following aggregation pipeline sorts the documents in descending order, so the documents with the greatest
pop value appear first, and limits the output to only the first five documents after sorting.
db.zips.aggregate([
{
$sort: {
pop: -1
}
},
{
$limit: 5
}
])