Saturday 21 February 2015

mongoDB : $push (aggregation)


$push operator returns an array of all values that result from applying an expression to each document in a group of documents that share the same group by key.

Syntax
{ $push: <expression> }

$addToSet is similar like push, but only difference is $addToSet don’t insert duplicate elements.

> 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 }
" : [ "cellphone", "cellphone", "cellphone" ] }


1. Get all the product categories by manufacturer wise
> db.products.aggregate([{"$group":{"_id": "$manufacturer", "categories" : {"$push" : "$category"}}}])
{ "_id" : "Samsung", "categories" : [ "cellphone", "cellphone", "cellphone" ] }
{ "_id" : "Apple", "categories" : [ "laptop", "laptop", "cellphone", "cellphone", "cellphone" ] }
{ "_id" : "Google", "categories" : [ "tablet", "tablet" ] }
{ "_id" : "DELL", "categories" : [ "laptop" ] }
{ "_id" : "Nokia", "categories" : [ "cellphone", "cellphone", "cellphone" ] }


As you observe the output,catgori”cellphone” is appeared thrice for the manufacturer Samsung, it is because, there are 3 products of category cellphone present products collection. But it is not the case with $addToSet, it don’t take duplicates.

> db.products.aggregate([{"$group":{"_id": "$manufacturer", "categories" : {"$addToSet" : "$category"}}}])
{ "_id" : "Samsung", "categories" : [ "cellphone" ] }
{ "_id" : "Apple", "categories" : [ "cellphone", "laptop" ] }
{ "_id" : "Google", "categories" : [ "tablet" ] }
{ "_id" : "DELL", "categories" : [ "laptop" ] }
{ "_id" : "Nokia", "categories" : [ "cellphone" ] }





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment