The aggregate() method in the
mongo shell provides a wrapper around the
aggregate database command. See the documentation for your
driver for a more idiomatic interface
for data aggregation operations.

The $group stage groups the documents of the zipcode
collection by the state field, calculates the totalPop field
for each state, and outputs a document for each unique state.

The new per-state documents have two fields: the _id field and
the totalPop field. The _id field contains the value of the
state; i.e. the group by field. The totalPop field is a
calculated field that contains the total population of each state. To
calculate the value, $group uses the $sum
operator to add the population field (pop) for each state.

After the $group stage, the documents in the
pipeline resemble the following:

The $match stage filters these grouped documents to
output only those documents whose totalPop value is greater than
or equal to 10 million. The $match stage does not alter
the matching documents but outputs the matching documents unmodified.

The first $group stage groups the documents by the
combination of city and state, uses the $sum
expression to calculate the population for each combination, and
outputs a document for each city and state combination.
[1]

After this stage in the pipeline, the documents resemble the
following:

A second $group stage groups the documents in the
pipeline by the _id.state field (i.e. the state field inside
the _id document), uses the $avg expression to calculate
the average city population (avgCityPop) for each state, and
outputs a document for each state.

The documents that result from this aggregation operation resembles the
following:

db.zipcodes.aggregate([{$group:{_id:{state:"$state",city:"$city"},pop:{$sum:"$pop"}}},{$sort:{pop:1}},{$group:{_id:"$_id.state",biggestCity:{$last:"$_id.city"},biggestPop:{$last:"$pop"},smallestCity:{$first:"$_id.city"},smallestPop:{$first:"$pop"}}},// the following $project is optional, and// modifies the output format.{$project:{_id:0,state:"$_id",biggestCity:{name:"$biggestCity",pop:"$biggestPop"},smallestCity:{name:"$smallestCity",pop:"$smallestPop"}}}])

The first $group stage groups the documents by the
combination of the city and state, calculates the sum of the pop values for each combination, and outputs a
document for each city and state combination.

The $sort stage orders the documents in the pipeline by
the pop field value, from smallest to largest; i.e. by
increasing order. This operation does not alter the documents.

The next $group stage groups the now-sorted documents
by the _id.state field (i.e. the state field inside the
_id document) and outputs a document for each state.

The stage also calculates the following four fields for each state.
Using the $last expression, the $group operator
creates the biggestCity and biggestPop fields that store the
city with the largest population and that population. Using the
$first expression, the $group operator creates
the smallestCity and smallestPop fields that store the city
with the smallest population and that population.