You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/30-simple-queries/5-combining-them-all.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,15 +10,15 @@ db.books.aggregate([
10
10
])
11
11
```
12
12
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))
Copy file name to clipboardExpand all lines: docs/30-simple-queries/6-writing-long-pipelines.mdx
+31-6Lines changed: 31 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,31 +4,31 @@ Aggregation pipelines can get very long, depending on how many stages we need to
4
4
5
5
This is why we should rewrite our last pipeline like this:
6
6
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))
let showOnlyTheseFields = {$project: {_id:0, title:1, year:1, totalInventory:1, available:1}};
20
20
let getJust15books = {$limit:15};
21
21
22
22
db.books.aggregate([
23
-
booksFrom1991With150pages,
23
+
booksFrom1985With150pages,
24
24
showOnlyTheseFields,
25
25
getJust15books,
26
26
]);
27
27
```
28
28
29
29
Easier to read, and reason about, right?
30
30
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
32
32
33
33
:::tip
34
34
@@ -40,4 +40,29 @@ Write your aggregation pipelines like you'll compose functions in your programmi
40
40
41
41
As this is code, we can even add comments (starting with `//`) to our pipelines
42
42
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}};
0 commit comments