Aggregation

Simple Aggregation Example

{
    "_id" : ObjectId("50b1aa983b3d0043b51b2c52"),
    "name" : "Nexus 7",
    "category" : "Tablets",
    "manufacturer" : "Google",
    "price" : 199
}

The following query find the number of products by category of the above collection :

db.products.aggregate([{$group:{_id:"$category",num_products:{$sum:1}}}])

The array represents the stage of the pipeline.