Skip to content

Commit 2ecb962

Browse files
committed
examples aggregation
1 parent 00a6717 commit 2ecb962

10 files changed

+173
-0
lines changed

custom-js/10limit.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use agg
2+
db.aggr.aggregate([
3+
{$match:
4+
{
5+
state:"NY"
6+
}
7+
},
8+
{$group:
9+
{
10+
_id: "$city",
11+
population: {$sum:"$pop"},
12+
}
13+
},
14+
{$project:
15+
{
16+
_id: 0,
17+
city: "$_id",
18+
population: 1,
19+
}
20+
},
21+
{$sort:
22+
{
23+
population:-1
24+
}
25+
},
26+
{$skip: 10},
27+
{$limit: 5}
28+
])
29+
30+

custom-js/1match.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use agg;
2+
db.aggr.aggregate([
3+
{$match:
4+
{
5+
state:"NY"
6+
}
7+
}
8+
]);

custom-js/2match_and_group.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use agg
2+
db.aggr.aggregate([
3+
{$match:
4+
{
5+
state:"NY"
6+
}
7+
},
8+
{$group:
9+
{
10+
_id: "$city",
11+
population: {$sum:"$pop"},
12+
zip_codes: {$addToSet: "$_id"}
13+
}
14+
},
15+
{$limit:5}
16+
])
17+
18+

custom-js/3match_group_and_project.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use agg
2+
db.aggr.aggregate([
3+
{$match:
4+
{
5+
state:"NY"
6+
}
7+
},
8+
{$group:
9+
{
10+
_id: "$city",
11+
population: {$sum:"$pop"},
12+
zip_codes: {$addToSet: "$_id"}
13+
}
14+
},
15+
{$project:
16+
{
17+
_id: 0,
18+
city: "$_id",
19+
population: 1,
20+
zip_codes:1
21+
}
22+
},
23+
{$limit : 5}
24+
])
25+
26+

custom-js/4using_avg.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use agg
2+
db.aggr.aggregate([
3+
{$group:
4+
{
5+
_id: "$state",
6+
avg_pop:{$avg:"$pop"}
7+
}
8+
}
9+
])
10+
11+

custom-js/5using_addToSet.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use agg
2+
db.products.aggregate([
3+
{$group:
4+
{
5+
_id: {
6+
"maker":"$manufacturer"
7+
},
8+
categories:{$addToSet:"$category"}
9+
}
10+
}
11+
])
12+
13+

custom-js/6using_max.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use agg
2+
db.aggr.aggregate([
3+
{$match:
4+
{
5+
state:"NY"
6+
}
7+
},
8+
{$group:
9+
{
10+
_id: "$city",
11+
max_pop:{$max:"$pop"}
12+
}
13+
},
14+
{$sort : {max_pop : -1}
15+
},
16+
{$limit : 3}
17+
])
18+
19+

custom-js/7using_push.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use agg
2+
db.products.aggregate([
3+
{$group:
4+
{
5+
_id: {
6+
"maker":"$manufacturer"
7+
},
8+
categories:{$push:"$category"}
9+
}
10+
}
11+
])
12+
13+

custom-js/8using_sum.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use agg
2+
db.products.aggregate([
3+
{$group:
4+
{
5+
_id: {
6+
"maker":"$manufacturer"
7+
},
8+
sum_prices:{$sum:"$price"}
9+
}
10+
}
11+
])
12+
13+

custom-js/9aggr.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use foo
2+
db.aggr.aggregate([
3+
/* get the population of every city in every state */
4+
{$group:
5+
{
6+
_id: {state:"$state", city:"$city"},
7+
population: {$sum:"$pop"},
8+
}
9+
} ,
10+
/* sort by state, population */
11+
{$sort:
12+
{"_id.state":1, "population":-1}
13+
},
14+
/* group by state, get the first item in each group */
15+
{$group:
16+
{
17+
_id:"$_id.state",
18+
city: {$first: "$_id.city"},
19+
population: {$first:"$population"}
20+
}
21+
}
22+
])

0 commit comments

Comments
 (0)