Skip to content

Commit ab11513

Browse files
authored
Merge branch 'main' into patch-47
2 parents fa7db80 + c24f97d commit ab11513

File tree

6 files changed

+95
-39
lines changed

6 files changed

+95
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
You can access the online version [here](https://mongodb-developer.github.io/aggregation-pipeline-lab/)
44

5-
This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator. We have a lab on how to use it available on https://mongodb-developer.github.io/docusaurus-workshop/.
5+
This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator. We have a lab on how to use it available on https://mongodb-developer.github.io/docusaurus-workshop/
66

77

docs/30-simple-queries/2-match.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ IF pages == 100 AND totalInventory == 1 {
133133
}
134134
```
135135

136-
👐 Return all the `books` from 2009 that have exactly 192 pages.
136+
👐 Return all the `books` from 2015 that have exactly 100 pages.
137137

138138
<details>
139139
<summary>Answer</summary>
@@ -146,8 +146,8 @@ IF pages == 100 AND totalInventory == 1 {
146146
{
147147
$match: {
148148
$and: [
149-
{ pages: 192 },
150-
{ year: 2009 }
149+
{ pages: 100 },
150+
{ year: 2015 }
151151
]
152152
}
153153
}
@@ -160,8 +160,8 @@ db.books.aggregate([
160160
{
161161
$match: {
162162
$and: [
163-
{ pages: 192 },
164-
{ year: 2009 }
163+
{ pages: 100 },
164+
{ year: 2015 }
165165
]
166166
}
167167
}
@@ -237,7 +237,7 @@ db.books.aggregate([
237237
</Tabs>
238238

239239

240-
👐 Return all the `books` from 2009 that have exactly 192 pages, using the shorthand `$and` notation:
240+
👐 Return all the `books` from 2015 that have exactly 100 pages, using the shorthand `$and` notation:
241241

242242
<details>
243243
<summary>Answer</summary>
@@ -248,14 +248,14 @@ db.books.aggregate([
248248
```js
249249
[
250250
{
251-
$match: {pages: 192, year: 2009}
251+
$match: {pages: 100, year: 2015}
252252
}
253253
]
254254
```
255255
</TabItem>
256256
<TabItem value="mongodb-shell" label="MongoDB Shell">
257257
```js
258-
db.books.aggregate([{$match: {pages: 192, year: 2009}}])
258+
db.books.aggregate([{$match: {pages: 100, year: 2015}}])
259259
```
260260
</TabItem>
261261
</Tabs>

docs/40-using-arrays/40-search-inside-objects-in-arrays.mdx

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ db.books.aggregate([
131131
</div>
132132
</details>
133133

134-
## $unwind
135-
136-
This is OK, but we get all attributes, although we're only interested in the MSRP!
137-
134+
This is okay, but We get a list of documents with a whole bunch of attributes which looks like the following:
138135

139136
```js
140137
{
@@ -168,7 +165,13 @@ This is OK, but we get all attributes, although we're only interested in the MSR
168165
]
169166
}
170167
```
171-
To improve, we'll use `$unwind`:
168+
169+
What if we are only interested in the MSRP?
170+
171+
There is more than one way to filter the unncessary keys and values:
172+
173+
### 1. Using $unwind aggregation stage
174+
See the following pipeline:
172175

173176
<Tabs groupId="aggregations">
174177
<TabItem value="atlas" label="Atlas UI">
@@ -225,4 +228,60 @@ db.books.aggregate([
225228

226229
You should get one document per attribute of the original book. All fields in these returned documents should be the same, except the ones in attributes.
227230

231+
### 🦸 2. Using $arrayToObject operator
232+
233+
We can also flatten the `attributes` array into an object using the `$arrayToObject` operator and then use a `$project` stage to filter unwanted fields.
234+
Note that `$arrayToObject` is not an aggregation stage.
235+
236+
<Tabs groupId="aggregations">
237+
<TabItem value="atlas" label="Atlas UI">
238+
239+
You need to select the `books` collection.
240+
241+
```js
242+
[
243+
{
244+
$match: {
245+
'attributes.key': 'msrp',
246+
'attributes.value': 9.99
247+
}
248+
},
249+
{
250+
$addFields: {
251+
attributes: {
252+
$arrayToObject: {
253+
$map: {
254+
input: '$attributes',
255+
as: 'attr',
256+
in: {
257+
k: '$$attr.key',
258+
v: '$$attr.value'
259+
}
260+
}
261+
}
262+
}
263+
}
264+
},
265+
{
266+
$project: { title: 1, 'attributes.msrp': 1 }
267+
}
268+
]
269+
```
270+
</TabItem>
271+
272+
<TabItem value="mongodb-shell" label="MongoDB Shell">
273+
274+
```js
275+
db.books.aggregate([
276+
{ $match: {_id: "0395623650"} },
277+
{ $unwind : "$attributes" },
278+
]);
279+
```
280+
281+
</TabItem>
282+
</Tabs>
283+
284+
🦸 Run the above aggregation to observe the difference in output as compared to using `$unwind`.
285+
286+
228287

docs/80-modifying-results/adding-fields.mdx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import TabItem from '@theme/TabItem';
77

88
# 👐 Adding New Fields to Results
99

10-
## $addFields
10+
## $set / $addFields
1111

12-
We want to estimate the reading time for a book. But we don't have that field stored in our data. We can use `$addFields` for this. If the field exists, it'll get updated, and if it doesn't, it's added.
12+
We want to estimate the reading time for a book. But we don't have that field stored in our data. We can use `$set` for this. If the field exists, it'll get updated, and if it doesn't, it's added.
1313

1414

1515
<Tabs groupId="aggregations">
@@ -22,7 +22,7 @@ We want to estimate the reading time for a book. But we don't have that field st
2222
pages: 1,
2323
}
2424
},
25-
{$addFields: {readingTimeHours: {$divide: [{$multiply: ["$pages", 2]}, 60]}}},
25+
{$set: {readingTimeHours: {$divide: [{$multiply: ["$pages", 2]}, 60]}}},
2626
]
2727
```
2828

@@ -37,7 +37,7 @@ db.books.aggregate([
3737
pages: 1,
3838
}
3939
},
40-
{$addFields: {readingTimeHours: {$divide: [{$multiply: ["$pages", 2]}, 60]}}},
40+
{$set: {readingTimeHours: {$divide: [{$multiply: ["$pages", 2]}, 60]}}},
4141
])
4242
```
4343

@@ -62,7 +62,7 @@ db.books.aggregate([
6262
pages: 1,
6363
}
6464
},
65-
{$addFields: {notes: "PLACEHOLDER"}}
65+
{$set: {notes: "PLACEHOLDER"}}
6666
]
6767
```
6868

@@ -78,19 +78,16 @@ db.books.aggregate([
7878
pages: 1,
7979
}
8080
},
81-
{$addFields: {notes: "PLACEHOLDER"}}
81+
{$set: {notes: "PLACEHOLDER"}}
8282
])
8383
```
8484

8585
</TabItem>
8686
</Tabs>
8787

88-
89-
9088
</div>
89+
</details>
9190

9291
:::info
93-
[$set](https://www.mongodb.com/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set) is an alias for $addFields that you'll find on many older posts and documentation.
94-
:::
95-
96-
</details>
92+
[$set](https://www.mongodb.com/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set) is a new alias for `$addFields`. You'll still find `$addFields` on many older posts and documentation.
93+
:::

docs/90-exporting-data/saving-to-collection.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can export the result of an aggregation pipeline to a different DB/collectio
1010
<Tabs groupId="aggregations">
1111
<TabItem value="atlas" label="Atlas UI">
1212

13-
Run this from the source collection
13+
Run this from the source collection:
1414

1515
```js
1616
[{ $out: { db: "<output-db>", coll: "<output-collection>" } }]
@@ -93,19 +93,19 @@ Reference: [📗 `$out` documentation](https://www.mongodb.com/docs/manual/refer
9393

9494
If the collection specified by the `$out` operation already exists, then the `$out` stage atomically replaces the existing collection with the new results collection upon completion of the aggregation.
9595

96-
To avoid overwriting the existing collection we can use `$merge` instead of `$out`.
96+
To avoid overwriting the existing collection, we can use `$merge` instead of `$out`.
9797

9898
```
9999
{ $merge : { into : "newCollection" } }
100100
```
101101

102-
- if the collection does not exists, it will be created
103-
- if it exists, new data will be added
104-
- if [a doc already exists](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/#std-label-merge-whenMatched), we can replace it, keep the existing one, merge both documents cause the stage to fail or run a pipeline.
102+
- If the collection does not exist, it will be created.
103+
- If it exists, new data will be added.
104+
- If [a doc already exists](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/#std-label-merge-whenMatched), we can replace it, keep the existing one, merge both documents, and cause the stage to fail or run a pipeline.
105105

106-
This is perfect for creating [On-Demand Materialized Views](https://www.mongodb.com/docs/manual/core/materialized-views/)
106+
This is perfect for creating [on-demand materialized views](https://www.mongodb.com/docs/manual/core/materialized-views/)
107107

108-
As an example, let's say we want the authors to contain all the books they've written, with all the book information. In this case, we'll do a `$lookup` to get the book information into the authors collection. We can even use the name `books` for the resulting data we're joining, shadowing the original `books` array we have in authors. This way it will look like the `books` array changes.
108+
As an example, let's say we want the authors to contain all the books they've written, with all the book information. In this case, we'll do a `$lookup` to get the book information into the authors collection. We can even use the name `books` for the resulting data we're joining, shadowing the original `books` array we have in authors. This way, it will look like the `books` array changes.
109109

110110
```js
111111
[
@@ -119,7 +119,7 @@ As an example, let's say we want the authors to contain all the books they've wr
119119
]
120120
```
121121

122-
Now a book will look like this. You can see that the books array has been "overwritten" by the `$lookup`.
122+
Now, a book will look like this. You can see that the books array has been "overwritten" by the `$lookup`.
123123

124124
```js
125125
{
@@ -249,7 +249,7 @@ We can go ahead and remove the authors from the books array, as it is redundant:
249249
]
250250
```
251251

252-
Now that our authors look the way we want, we can overwrite the authors collection using `$merge`
252+
Now that our authors look the way we want, we can overwrite the authors collection using `$merge`.
253253

254254
```js
255255
[
@@ -269,9 +269,9 @@ Now that our authors look the way we want, we can overwrite the authors collecti
269269
]
270270
```
271271

272-
- we use the `_id` field to match documents
273-
- we replace the existing ones with `replace`
272+
- We use the `_id` field to match documents.
273+
- We replace the existing ones with `replace`.
274274

275275
:::warning
276276
We should see a message telling us that the $merge operator will cause the pipeline to persist the results to the specified location. This stage changes data.
277-
:::
277+
:::

docs/lecture-material.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
sidebar_position: 90
33
---
44

5-
# 📘 Lecture material
5+
# 📘 Lecture Material
66

77
|||
88
|-|:-|

0 commit comments

Comments
 (0)