Aggregation tasks

Aggregation queries are constructed from a set of aggregation tasks.

Generally tasks can occur in any number and sequence, if they are syntactically valid. Tasks can be divided into two task types:
  • Field producer tasks that act on field attributes and determine the field content of the output documents.
  • Scope tasks that act on documents and determine which documents can be included in the result set, and their order.
Table 1. Aggregation tasks and their types
Task Type Description
$distinct Producer Retrieve distinct values for specified fields
$group Producer Group values for specified fields
$limit Scope Return at most the specified number of documents from the result set
$match Scope Filter documents
$project Producer Select attributes
$skip Scope Skip the specified number of rows
$sort Scope Order the result set ascending or descending
$unwind Producer Returns a duplicated document for each value in the specified array.

In general, an aggregation task produces an intermediate result set that serves as input for the next task. However, for optimization, queries are automatically rewritten during run time to combine tasks, if possible.

The following example shows an aggregation query that groups sales per author and sorts the sales in descending order:

Example 1:
db.books.aggregate(
   {$group: {_id: “author”, sales: {$sum: 1}}},
   {$sort: {sales: -1}}
) 
Sample output:
Row 1: {_id: "Doe, John", sales: 12034 }
Row 2: {_id: "Miller, Ann", sales: 10010 )
Row 3: {_id: "Smith, Bob", sales: 6320 }
Query examples
db.furniture.aggregate({"$project":{"type":1,"material":1,"count":1,"color":1}}, 
                       {"$sort":{"material":-1,"type":1}}, 
                       {"$group":{"_id":{"type":"$type"},"biggest":{"$max":"color"}}})

db.furniture.aggregate({"$group":{"_id":{"atype":"$type","acolor":{"$toLower":"$color"}},"count":{"$sum":1}}},
                       {"$project":{"ptype":"$_id.atype","count":1,"_id.acolor":1}})


db.furniture.aggregate({"$group":{"_id":{"type":"$type","color":"$color"},"countavg":{"$avg":"$count"}}},
                       {"$sort":{"_id.color":-1}}, 
                       {"$skip":2}, 
                       {"$group":{"_id":"_id.color","totalavg":{"$avg":"$countavg"}}}, 
                       {"$project":{"tcolor":"_id","total":"totalavg"}})