MongoDB Documentation, Release 3.0.4 { "_id" : "MN", "avgCityPop" : 5335 } See also: $group , $sum , $avg The following aggregation operation returns the smallest and largest cities by population for each state: Return Largest and Smallest Cities by State In this example, the aggregation pipeline (page 440) consists of a $group stage, a $sort stage, another $group stage, and a $project stage: At this stage in the pipeline, the documents resemble the following: 7.3. Aggregation Examples 455 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. { "_id" : { "state" : "CO", "city" : "EDGEWATER" }, "pop" : 13154 }