Skip to content

Commit 190529a

Browse files
committed
Added group, reorg
1 parent 84db62b commit 190529a

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

docs/70-grouping/_category_.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Grouping results",
3+
"position": 70,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Grouping."
7+
}
8+
}

docs/70-grouping/group.mdx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Group
2+
3+
We can sum all the pages for all books in each year:
4+
5+
```js
6+
db.books.aggregate([
7+
{$group:{
8+
_id: "$year",
9+
totalPages: {$sum: "$pages"}
10+
}}
11+
])
12+
```
13+
14+
Here, we group by book's `year` (the _id of the grouping), and then we create a new field called `totalPages` that's the sum of all pages.
15+
16+
💻 Can you get the results ordered ascending by year?
17+
18+
<details>
19+
<summary>Answer</summary>
20+
<div>
21+
22+
```js
23+
db.books.aggregate([
24+
{$group:{
25+
_id: "$year",
26+
totalPages: {$sum: "$pages"}
27+
}},
28+
{$sort: {_id: 1}}
29+
])
30+
```
31+
</div>
32+
</details>
33+
34+
💻 Can you get the average pages per year (hint: use `$avg`)?
35+
36+
<details>
37+
<summary>Answer</summary>
38+
<div>
39+
40+
```js
41+
db.books.aggregate([
42+
{$group:{
43+
_id: "$year",
44+
totalPages: {$avg: "$pages"}
45+
}},
46+
{$sort: {_id: 1}}
47+
])
48+
```
49+
</div>
50+
</details>
51+
52+
💻 Which year had the most printed out pages?
53+
54+
<details>
55+
<summary>Answer</summary>
56+
<div>
57+
58+
```js
59+
db.books.aggregate([
60+
{$group:{
61+
_id: "$year",
62+
totalPages: {$sum: "$pages"}
63+
}},
64+
{$sort: {totalPages: -1}},
65+
{$limit: 1}
66+
])
67+
```
68+
</div>
69+
</details>

docs/70-modifying-results/_category_.json renamed to docs/90-modifying-results/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Modifying results",
3-
"position": 70,
3+
"position": 90,
44
"link": {
55
"type": "generated-index",
66
"description": "Changing the output of our pipelines."
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)