Skip to content

Commit 21044b1

Browse files
committed
Fixed examples for new data
1 parent c2a53e5 commit 21044b1

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

docs/30-simple-queries/5-combining-them-all.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ db.books.aggregate([
1010
])
1111
```
1212

13-
💻 Get 15 books from 1991 with 150 pages. Show only the `title`, `year`, `totalInventory` and `available` books. (If you don't remember which fields we have, the sample doc is [here](/docs/simple-queries/project))
13+
💻 Get 15 books from 1985 with 150 pages. Show only the `title`, `year`, `totalInventory` and `available` books. (If you don't remember which fields we have, the sample doc is [here](/docs/simple-queries/project))
1414

1515
<details>
1616
<summary>Answer</summary>
1717
<div>
1818

1919
```js
2020
db.books.aggregate([
21-
{$match: {year: 1991, pages: 150}}, {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}}, {$limit: 15}
21+
{$match: {year: 1985, pages: 150}}, {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}}, {$limit: 15}
2222
])
2323
```
2424
</div>

docs/30-simple-queries/6-writing-long-pipelines.mdx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ Aggregation pipelines can get very long, depending on how many stages we need to
44

55
This is why we should rewrite our last pipeline like this:
66

7-
Get 15 books from 1991 with 150 pages. Show only the `title`, `year`, `totalInventory` and `available` books. (Sample doc [here](/docs/simple-queries/project))
7+
Get 15 books from 1985 with 150 pages. Show only the `title`, `year`, `totalInventory` and `available` books. (Sample doc [here](/docs/simple-queries/project))
88

99
```js
1010
db.books.aggregate([
11-
{$match: {year: 1991, pages: 150}}, {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}}, {$limit: 15}
11+
{$match: {year: 1985, pages: 150}}, {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}}, {$limit: 15}
1212
])
1313
```
1414
Will be changed into:
1515

1616

1717
```js
18-
let booksFrom1991With150pages = {$match: {year: 1991, pages: 150}};
18+
let booksFrom1985With150pages = {$match: {year: 1985, pages: 150}};
1919
let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}};
2020
let getJust15books = {$limit: 15};
2121

2222
db.books.aggregate([
23-
booksFrom1991With150pages,
23+
booksFrom1985With150pages,
2424
showOnlyTheseFields,
2525
getJust15books,
2626
]);
2727
```
2828

2929
Easier to read, and reason about, right?
3030

31-
💻 Try to run the above pipeline and compare your results: should be the same
31+
💻 Try to run the above pipeline and compare your results: should be the same as before
3232

3333
:::tip
3434

@@ -40,4 +40,29 @@ Write your aggregation pipelines like you'll compose functions in your programmi
4040

4141
As this is code, we can even add comments (starting with `//`) to our pipelines
4242

43-
:::
43+
:::
44+
45+
46+
💻 We can also use $gte to get the books with 150 pages or more. Check $gte syntax in the [docucumentation](https://www.mongodb.com/docs/manual/reference/operator/query/gte/) and write an aggregation pipeline to return 15 books from 1985 with more than 150 pages. Show only the `title`, `year`, `totalInventory` and `available` books. (Sample doc [here](/docs/simple-queries/project))
47+
48+
_Hint: _ we'll need to use `$and` as maybe this is a bit more complex.
49+
50+
<details>
51+
<summary>Answer</summary>
52+
<div>
53+
54+
```js
55+
let moreThan150pages = {pages: {$gte: 150}}
56+
let booksFrom1985WithMoreThan150pages = {$match: {$and: [{year: 1985}, moreThan150pages]}};
57+
let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}};
58+
let getJust15books = {$limit: 15};
59+
60+
db.books.aggregate([
61+
booksFrom1985With150pages,
62+
showOnlyTheseFields,
63+
getJust15books,
64+
]);
65+
```
66+
67+
</div>
68+
</details>

docs/30-simple-queries/7-repeating-stages.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ From the [Aggregation Pipelines manual](https://www.mongodb.com/docs/manual/refe
99

1010
> All stages except the $out, $merge, $geoNear, $changeStream, and $changeStreamSplitLargeEvent stages can appear multiple times in a pipeline.
1111
12-
So we can repeat most stages, and do something like this to get all books from 2011 with 100 pages (although it can make sense or not)
12+
So we can repeat most stages, and do something like this to get all books from 1985 with more than 100 pages (although it can make sense or not)
1313

1414
```js
15-
db.books.aggregate([{$match: {pages: 100}}, {$match: {year: 2011}}])
15+
db.books.aggregate([{$match: {pages: {$gte: 100}}}, {$match: {year: 1985}}])
1616
```
1717

1818

@@ -25,7 +25,7 @@ db.books.aggregate([{$match: {pages: 100}}, {$match: {year: 2011}}])
2525

2626
```js
2727
db.books.aggregate([
28-
{$match: {pages: 100}},
28+
{$match: {pages: {$gte: 100}}},
2929
{$match: {year: 2011}},
3030
{$limit: 1},
3131
{$limit: 1},

0 commit comments

Comments
 (0)