Saturday, 21 February 2015

mongoDB : Aggregation pipeline


Aggregation pipeline framework works exactly as pipelines in unix. In pipeline frame work, documents enter a multi-stage pipeline that transforms the documents into aggregated results. MongoDB aggregation pipeline consist of stages. Each stage transforms document as they pass through the pipeline.

Let’s say I had below documents in products collection.

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.insert({"_id" : 9, "name": "Samsung Galaxy S2, White", "category" :"cellphone", "manufacturer" : "Samsung", "price": 469})
db.products.insert({"_id" : 10, "name": "Google Nexus 10 tablet", "category" :"tablet", "manufacturer" : "Google", "price": 254})
db.products.insert({"_id" : 11, "name": "Nokia x2", "category" :"cellphone", "manufacturer" : "Nokia", "price": 93})
db.products.insert({"_id" : 12, "name": "Apple iPhone 4", "category" :"cellphone", "manufacturer" : "Apple", "price": 121})
db.products.insert({"_id" : 13, "name": "Apple iPhone 5", "category" :"cellphone", "manufacturer" : "Apple", "price": 409})
db.products.insert({"_id" : 14, "name": "Apple iPhone 6", "category" :"cellphone", "manufacturer" : "Apple", "price": 695})

> 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 }
{ "_id" : 9, "name" : "Samsung Galaxy S2, White", "category" : "cellphone", "manufacturer" : "Samsung", "price" : 469 }
{ "_id" : 10, "name" : "Google Nexus 10 tablet", "category" : "tablet", "manufacturer" : "Google", "price" : 254 }
{ "_id" : 11, "name" : "Nokia x2", "category" : "cellphone", "manufacturer" : "Nokia", "price" : 93 }
{ "_id" : 12, "name" : "Apple iPhone 4", "category" : "cellphone", "manufacturer" : "Apple", "price" : 121 }
{ "_id" : 13, "name" : "Apple iPhone 5", "category" : "cellphone", "manufacturer" : "Apple", "price" : 409 }
{ "_id" : 14, "name" : "Apple iPhone 6", "category" : "cellphone", "manufacturer" : "Apple", "price" : 695 }


1.Get all products where manufacturer is Apple, group by category.
db.products.aggregate([
{"$match": {"manufacturer": "Apple"}}, 
{"$group" : {"_id" : "$category","Total Items" : {"$sum" :1}}}
])


First we need to find all the products where “manufacturer” is “Apple” and next we need to pass the output of match phase to group phase.

> db.products.aggregate([
... {"$match": {"manufacturer": "Apple"}},
... {"$group" : {"_id" : "$category","Total Items" : {"$sum" :1}}}
... ])
{ "_id" : "cellphone", "Total Items" : 3 }
{ "_id" : "laptop", "Total Items" : 2 }


2.Get all products where manufacturer is Apple, group by category and sort by total items.

db.products.aggregate([ 
{"$match": {"manufacturer": "Apple"}},  
{"$group" : {"_id" : "$category","Total Items" : {"$sum" :1}}}, 
{"$sort":{"Total Items":1}} 
])


First we need to find all the products where “manufacturer” is “Apple” and next we need to pass the output of match phase to group phase, then to sort phase.


> db.products.aggregate([ {"$match": {"manufacturer": "Apple"}},  {"$group" : {"_id" : "$category","Total Items" : {"$sum" :1}}}, {"$sort":{"Total Items":1}} ])
{ "_id" : "laptop", "Total Items" : 2 }
{ "_id" : "cellphone", "Total Items" : 3 }


 

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment