Saturday, 21 February 2015

mongoDB : Aggregations


Aggregation operations are used to group values from multiple documents. Aggregations provide various operations like grouping, sum, average etc.

Let’s say I products collection has below data.
db.products.insert({"_id" : 1, "name": "Nokia X3", "category" :"cellphone", "manufacturer" : "Nokia", "price": 103})
db.products.insert({"_id" : 2, "name": "Dell Inspiron i3147-3750", "category" :"laptop", "manufacturer" : "DELL", "price": 359})
db.products.insert({"_id" : 3, "name": "Nokia Lumia 521", "category" :"cellphone", "manufacturer" : "Nokia", "price": 65})
db.products.insert({"_id" : 4, "name": "Google Nexus 9 tablet", "category" :"tablet", "manufacturer" : "Google", "price": 194})
db.products.insert({"_id" : 5, "name": "Apple MacBook Air MD711LL/B", "category" :"laptop", "manufacturer" : "Apple", "price": 854})
db.products.insert({"_id" : 6, "name": "Apple MacBook Air MD760LL/B", "category" :"laptop", "manufacturer" : "Apple", "price": 859})
db.products.insert({"_id" : 7, "name": "Samsung Galaxy S III", "category" :"cellphone", "manufacturer" : "Samsung", "price": 199})
db.products.insert({"_id" : 8, "name": "Samsung Galaxy S5, White", "category" :"cellphone", "manufacturer" : "Samsung", "price": 569})

> db.products.find()
{ "_id" : 1, "name" : "Nokia X3", "category" : "cellphone", "manufacturer" : "Nokia", "price" : 103 }
{ "_id" : 2, "name" : "Dell Inspiron i3147-3750", "category" : "laptop", "manufacturer" : "DELL", "price" : 359 }
{ "_id" : 3, "name" : "Nokia Lumia 521", "category" : "cellphone", "manufacturer" : "Nokia", "price" : 65 }
{ "_id" : 4, "name" : "Google Nexus 9 tablet", "category" : "tablet", "manufacturer" : "Google", "price" : 194 }
{ "_id" : 5, "name" : "Apple MacBook Air MD711LL/B", "category" : "laptop", "manufacturer" : "Apple", "price" : 854 }
{ "_id" : 6, "name" : "Apple MacBook Air MD760LL/B", "category" : "laptop", "manufacturer" : "Apple", "price" : 859 }
{ "_id" : 7, "name" : "Samsung Galaxy S III", "category" : "cellphone", "manufacturer" : "Samsung", "price" : 199 }
{ "_id" : 8, "name" : "Samsung Galaxy S5, White", "category" : "cellphone", "manufacturer" : "Samsung", "price" : 569 }


1. Group all products by category
> db.products.aggregate([{$group: {"_id" : "$category", "Total Items" : {$sum : 1}}}])
{ "_id" : "laptop", "Total Items" : 3 }
{ "_id" : "tablet", "Total Items" : 1 }
{ "_id" : "cellphone", "Total Items" : 4 }


2. Group allproducts by manufacturer
> db.products.aggregate([{$group: {"_id" : "$manufacturer", "Total Items" : {$sum : 1}}}])
{ "_id" : "Samsung", "Total Items" : 2 }
{ "_id" : "Apple", "Total Items" : 2 }
{ "_id" : "Google", "Total Items" : 1 }
{ "_id" : "DELL", "Total Items" : 1 }
{ "_id" : "Nokia", "Total Items" : 2 }


3. Group allproducts by manufacturer and category
> db.products.aggregate([{$group: {"_id" : {"manufacturer":"$manufacturer", "category": "$category"}, "Total Items" : {$sum : 1}}}])
{ "_id" : { "manufacturer" : "Samsung", "category" : "cellphone" }, "Total Items" : 2 }
{ "_id" : { "manufacturer" : "Apple", "category" : "laptop" }, "Total Items" : 2 }
{ "_id" : { "manufacturer" : "Google", "category" : "tablet" }, "Total Items" : 1 }
{ "_id" : { "manufacturer" : "DELL", "category" : "laptop" }, "Total Items" : 1 }
{ "_id" : { "manufacturer" : "Nokia", "category" : "cellphone" }, "Total Items" : 2 }



Don’t worry much about the syntax, I will explain about each operator like group and sum in detail, in subsequent posts.

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment