Mongodb Aggregation

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

$project reshape - n:1
$match filter - n:1
$group aggregate - n:1
$sort  sort - 1 : 1
$skip  skips - n:1
$limit limit - n:1
$unwind normalize the data
tags :["red","blue"]
tags : red
tags : blue
$out : output - 1:1
$redact


compount groupby aggregate function

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

seçili markaların ürün fiyatlarını toplama

db.products.aggregate([
{$group:{
_id:{"maker": "$manufacturer"},
sum_prices: {$sum:"$price"}
}
}
])

json dosyalarını almak için mongoimport kullanıyoruz

//state göre toplam nüfus
db.zips.aggregate([{"$group":{"_id":"$state", "population":{$sum:"$pop"}}}])

//state göre ortalama nüfus
db.zips.aggregate([{"$group":{"_id":"$state", "average_pop":{$avg:"$pop"}}}])

// _id yerine başka bir kolon ismi kullanmak için {} içine alıp isim veriyoruz
bu sayede her markanın dahil olduğu kategorileri görüyoruz
db.products.aggregate([
{$group:{
_id:{"maker":"$manufacturer"},
categories: {$addToSet:$category}
}
}
])

yeni bir kategori yaratıp uniqueleri buluyoruz
db.zip.aggregate([
{$group:{
_id:"$city",
postal_codes: {$addToSet:$pop}
}
}
])


db.products.aggregate([
{$group:{
_id:{"maker":"$manufacturer"},
categories: {$push:$category}
}
}
])


db.products.aggregate([
{$group:{
_id:{"maker":"$manufacturer"},
maxprice: {$max:"$price"}
}
}
])

db.zips.aggregate([{$group:{"_id":"$state", pop:{$max:"$pop"}}}])


db.grades.aggregate([ {'$group': {_id: {class_id:"$class_id", student_id: "$student_id"}, 'avarage' : {"$avg":"$score" }
}
},
{'$group' : { _id: "$_id.class_id", 'avarage' : {"$avg" : "$average"} } }
+++

> db.fun.find()
{ "_id" : 0, "a" : 0, "b" : 0, "c" : 21 }
{ "_id" : 1, "a" : 0, "b" : 0, "c" : 54 }
{ "_id" : 2, "a" : 0, "b" : 1, "c" : 52 }
{ "_id" : 3, "a" : 0, "b" : 1, "c" : 17 }
{ "_id" : 4, "a" : 1, "b" : 0, "c" : 22 }
{ "_id" : 5, "a" : 1, "b" : 0, "c" : 5 }
{ "_id" : 6, "a" : 1, "b" : 1, "c" : 87 }
{ "_id" : 7, "a" : 1, "b" : 1, "c" : 97 }

db.fun.aggregate([{$group:{_id:{a:"$a", b:"$b"}, c:{$max:"$c"}}}, {$group:{_id:"$_id.a", c:{$min:"$c"}}}])
])

52 and 22 result of this query

In this assignment you will use the aggregation framework to find the most frequent author of comments on your blog. We will be using a data set similar to ones we've used before.
Start by downloading the handout zip file for this problem. Then import into your blog database as follows:
mongoimport --drop -d blog -c posts posts.json
Now use the aggregation framework to calculate the author with the greatest number of comments.
To help you verify your work before submitting, the author with the fewest comments is Mariela Sherer and she commented 387 times.
Please choose your answer below for the most prolific comment author:

Crunching the Zipcode dataset
Please calculate the average population of cities in California (abbreviation CA) and New York (NY) (taken together) with populations over 25,000.

For this problem, assume that a city name that appears in more than one state represents two separate cities.

Please round the answer to a whole number.
Hint: The answer for CT and NJ (using this data set) is 38177.

Please note:
Different states might have the same city name.
A city might have multiple zip codes.


For purposes of keeping the Hands On shell quick, we have used a subset of the data you previously used in zips.json, not the full set. This is why there are only 200 documents (and 200 zip codes), and all of them are in New York, Connecticut, New Jersey, and California.

If you prefer, you may download the handout and perform your analysis on your machine with


db.zips.aggregate([
 { "$match" : { "$or" : [ { "state" : "CA" },{ "state" :"NY" } ] }}

,{ "$group" : { "_id" : { "state" : "$state", "city" : "$city"},
                      "pop"  : { "$sum" : "$pop"}} }

,{ "$match" : { "pop" : { "$gt" : 25000 }}}

,{ "$group" : { "_id" : null,
                      "avg" : { "$avg" : "$pop"}} }
])

http://webappdevm.blogspot.com.tr/2014/11/mongodb-course-for-developers-unit-58_29.html

db.posts.aggregate([
 { "$project" : { "author" : "$comments.author"}}
,{ "$unwind"  : "$author"}
,{ "$group"   : { "_id"            : "$author",
      "numPosts" : { "$sum" : 1} }}
,{ "$sort" : { "numPosts" :-1}}
,{"$limit" : 1}
])


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


db.grades.aggregate([
 { "$unwind" : "$scores" }

,{ "$match"  : { "$or"    : [ { "scores.type" : "exam" },{ "scores.type" : "homework" }]} }

,{ "$group"  : { "_id"    : { "class" : "$class_id", "student" : "$student_id" },
         "stdAvg" : {"$avg" : "$scores.score" } } }

,{ "$group"  : { "_id"    : "$_id.class",
         "avg1"   : { "$avg" : "$stdAvg"}  } }

,{ "$project" : { "_id"   : false, "class": "$_id", "avg" : "$avg1" } }

,{ "$sort"   : { "avg"    : -1} }

,{ "$limit"  : 1}
])
Below, choose the class_id with the highest average student average.

aggregation on nodejs


db.companies.aggregate([
{$match: {founded_year : 2005}},
{$sort: {name :1 }}
{$limit: 5},
{$project : { _id:0, name:1, founded_year:1}}
])

----unwind

db.companies.aggregate([
{$match: {founded_year : 2005}},
{$unwind: "$funding_rounds"}
{$project : { _id:0, name:1, founded_year:1, amount: "$funding_rounds.raised_amonunt"}}
])

accumulator
db.companies.aggregate([
{$match : {"funding_rounds" : {$exists : true, $ne: [] } } },
{$project : {_id : 0, name : 1, largest_round : {$max : "funding_rounds.raised_amount" } }}
])

group by örneği

db.companies.aggregate([
{$group: { _id : {founded_year: "$founded_year"}, average_number_of_employess : {$avg: "$number_of_employees"} }},
{$sort : {a:-1}}
])


db.companies.aggregate([
{$match: {"relationships.person" : {$ne: null} }},
{$project : { relationships : 1, _id : 0}},
{$unwind : "$relationships"},
{$group : {_id : "$relationships.person", count : { $sum : 1}}},
{$sort : {count: -1}}
])

Yorumlar

  1. Merhaba,

    Aggregate kullanımıyla ilgili sorunlar yaşıyorum bu konuda ücretli destek sağlıyormusunuz.

    Sağlıyor iseniz 0541 311 92 77 numaramdan ulaşır iseniz gerçekten çok sevinirim.

    YanıtlaSil

Yorum Gönder

Bu blogdaki popüler yayınlar

22.06.2020 - 26.06.2020 arası işler

Asp.net RestSharp ile data post etmek

List Box Item içindeki elemanları aşağı veya yukarı taşımak