Friday 30 August 2013

mongo shell aggregate framework examples

!!!!!!!!!!!!!!!!
Group
!!!!!!!!!!!!!!!!

db.products.aggregate([ {$group: {_id:"$category" , num_products : {$sum:1} }} ])
will return document "result" that have ids of categories that there is in products collection with field num_products with value of number of items of specified category

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

db.products.aggregate([ {$group: {_id:"$manufacturer" , sum_prices: {$sum:"$price"} }} ])
calculation of sum of totall price of devices of produced by every manufacturer

db.products.aggregate([ {$group: {_id:"$manufacturer" , avg_price: {$avg:"$price"} }} ])
calculation of the average price of devices of produced by every manufacturer

db.products.aggregate([ {$group: {_id:"$manufacturer" , categories: {$push:"$category"} }} ])
getting collection groupped by manufacturer with array (set) of categories where every category as many time in array as many such category of such manufacturer is exists in collection

db.products.aggregate([ {$group: {_id:"$manufacturer" , categories: {$push:"$category"} }} ])
getting collection groupped by manufacturer with array (set) of categories where every category as many time in array as many such category of such manufacturer is exists in collection

db.zips.aggregate([{$group:{_id:"$state","pop": {$max:"$pop"}}}])
The aggregation query that will return the population of the postal code in each state with the highest population.

db.group.aggregate([{
$group:{_id:   { class_id: "$class_id" , student_id :  "$student_id" } , average : {"$avg"  : "$score"}}} ,
{$group: {_id : "$_id.class_id" , average :{ "$avg" : "$average"}}
}])
The two step group example, on the first step we creating collection with unique pairs class_id and student_id evaluate average score for this pairs, on the second step we getting calculatting average score of certain student for certain class

!!!!!!!!!!!!!!!!
Project
!!!!!!!!!!!!!!!!
db.zips.aggregate([{$project:{_id:0, city:{$toLower:"$city"}, pop:1, state:1, zip:"$_id"}}])

projection example:
from such collection:
{
"city" : "ACMAR",
"loc" : [
-86.51557,
33.584132
],
"pop" : 6055,
"state" : "AL",
"_id" : "35004"
}

will return such:
{
"city" : "acmar",
"pop" : 6055,
"state" : "AL",
"zip" : "35004"
}

db.products.aggregate([ {$group: {_id:"$manufacturer" , sum_prices: {$sum:"$price"} }} ])
calculation of sum of totall price of devices of produced by every manufacturer

db.products.aggregate([ {$group: {_id:"$manufacturer" , avg_price: {$avg:"$price"} }} ])
calculation of the average price of devices of produced by every manufacturer


!!!!!!!!!!!!!!!!
Match
!!!!!!!!!!!!!!!!

db.zips.aggregate([{ $match : {state: NY} }])
kind of fillter, will return only documents where state is NY

!!!!!!!!!!!!!!!!!!!!!!!!!!!
Sort, Skip and Limit
!!!!!!!!!!!!!!!!!!!!!!!!!!!

db.zips.aggregate([{ $sort: {state : 1} }  , {$skip:10} , {$limit:5}]) 
query with just a sort stage to sort by state, ascending, skip the first 10 and get only next 5

!!!!!!!!!!!!!!!!
First and Last
!!!!!!!!!!!!!!!!

db.fun.aggregate([{$sort:{c:1}}, {$group:{_id:"$a", c:{$first:"$c"}}}])
will return collection of different "a" and for "c" key for the document from this collection will be the smallest "c" value for the corresponded "a"

!!!!!!!!!!!!!!!!
Unwind
!!!!!!!!!!!!!!!!

db.posts.aggregate([{ $unwind:"$comments"}]) 
from the posts collection will return new collection in which for every array element in comments arrat will be create new document

No comments:

Post a Comment