Skip to content

Commit 9fb00b7

Browse files
Merge pull request #17 from bohyunjung/main
Update lab material
2 parents 1b84624 + 3c4e1d9 commit 9fb00b7

File tree

8 files changed

+67
-63
lines changed

8 files changed

+67
-63
lines changed

docs/40-CRUD/1-WHERE.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Now, translate the following into a MongoDB query.
115115
<summary>Answer</summary>
116116
<div>
117117
```js
118-
db.books.find({ genres: "Science", pages: {$gt: 300} });
118+
db.books.find({ "genre.name": "Science", pages: {$gt: 300} });
119119
```
120120
</div>
121121
</details>

docs/40-CRUD/2-SELECT.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Here:
5555
## **Example 3: Using projection along with a query**
5656

5757
```js
58-
db.books.find({ genres: "Science" }, { title: 1, totalInventory: 1, _id: 0 });
58+
db.books.find({ "genre.name": "Science" }, { title: 1, totalInventory: 1, _id: 0 });
5959
```
6060

6161
**Equivalent SQL query:**
@@ -90,7 +90,7 @@ Here:
9090
<summary>Answer</summary>
9191
<div>
9292
```js
93-
db.books.find({genres: "History"}, {_id: 0, authors: 0});
93+
db.books.find({ "genre.name": "History" }, { _id: 0, authors: 0 });
9494
```
9595
</div>
9696
</details>

docs/40-CRUD/3-ORDER-LIMIT.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ This fetches the **5 books with the highest stock**.
3737

3838
```js
3939
db.books
40-
.find({ genres: "Fiction" }, { title: 1, pages: 1 })
40+
.find({ "genre.name": "Fiction" }, { title: 1, pages: 1 })
4141
.sort({ pages: -1 })
4242
.limit(10);
4343
```

docs/50-aggregation/3-sort-limit.mdx

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5;
7272

7373
## 👐 Challenge
7474

75-
### 👐 1. After the year 2010, which book has the most number of authors?
75+
### 👐 1. After the year 2000, which book has the most number of authors?
7676

7777
<details>
7878
<summary>Answer</summary>
@@ -89,44 +89,51 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
8989
<TabItem value="mongodb-shell" label="Using $project">
9090
```js
9191
db.books.aggregate([
92-
{
93-
$match: { year: {$gt: 2010}}
94-
},
95-
{
96-
$project: {
97-
title: 1,
98-
authors: 1,
99-
numAuthors: {$size: "$authors"},
100-
_id: 0
101-
}
102-
},
103-
{
104-
$sort: { "numAuthors": -1}
105-
},
106-
{
107-
$limit: 1
108-
}
109-
]);
110-
```
111-
</TabItem>
112-
<TabItem value="atlas" label="Using $addFields">
113-
```js
114-
db.books.aggregate([
11592
{
116-
$match: { year: {$gt: 2010}}
93+
$match: { year: { $gt: 2000 } }
94+
},
95+
{
96+
$match: {
97+
authors: { $exists: true },
98+
}
11799
},
118100
{
119101
$addFields: {
120-
numAuthors: {$size: "$authors"},
102+
numAuthors: { $size: "$authors" },
121103
}
122104
},
123105
{
124-
$sort: { "numAuthors": -1}
106+
$sort: { "numAuthors": -1 }
125107
},
126108
{
127109
$limit: 1
128110
}
129111
]);
112+
```
113+
</TabItem>
114+
<TabItem value="atlas" label="Using $addFields">
115+
```js
116+
db.books.aggregate([
117+
{
118+
$match: { year: { $gt: 2000 } }
119+
},
120+
{
121+
$match: {
122+
authors: { $exists: true },
123+
}
124+
},
125+
{
126+
$addFields: {
127+
numAuthors: { $size: "$authors" },
128+
}
129+
},
130+
{
131+
$sort: { "numAuthors": -1 }
132+
},
133+
{
134+
$limit: 1
135+
}
136+
]);
130137
```
131138
</TabItem>
132139
</Tabs>

docs/50-aggregation/4-group.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ GROUP BY year;
137137
db.reviews.aggregate([
138138
{
139139
$group: {
140-
_id: "$bookId",
140+
_id: "$_id.bookId",
141141
avgRating: { $avg: "$rating" }
142142
}
143-
},
143+
}
144144
]);
145145
```
146146

docs/50-aggregation/5-lookup.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ The $lookup operation creates an array within each book document. Using $unwind
9999
db.books.aggregate([
100100
{
101101
$lookup:
102-
{
103-
from: "reviews",
104-
localField: "_id",
105-
foreignField: "bookId",
106-
as: "reviews"
107-
}
102+
{
103+
from: "reviews",
104+
localField: "_id",
105+
foreignField: "_id.bookId",
106+
as: "reviews"
107+
}
108108
}
109109
]);
110110
```

docs/50-aggregation/7-merge.mdx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ The `$merge` stage enables you to store aggregation results into a different col
1313

1414
### **Key features:**
1515

16-
✔️ Inserts new documents if they don’t exist
17-
✔️ Updates existing documents based on `_id` or a specified field
18-
✔️ Can replace, merge, or discard duplicate records
19-
✔️ Useful for **ETL workflows, reporting tables, and maintaining summary data**
16+
- ✔️ Inserts new documents if they don’t exist
17+
- ✔️ Updates existing documents based on `_id` or a specified field
18+
- ✔️ Can replace, merge, or discard duplicate records
19+
- ✔️ Useful for **ETL workflows, reporting tables, and maintaining summary data**
2020

2121
---
2222

@@ -51,8 +51,8 @@ The `$merge` stage enables you to store aggregation results into a different col
5151

5252
```js
5353
db.books.aggregate([
54-
{ $unwind: "$genres" },
55-
{ $group: { _id: "$genres", totalBooks: { $sum: 1 } } },
54+
{ $unwind: "$genre" },
55+
{ $group: { _id: "$genre.genreId", totalBooks: { $sum: 1 } } },
5656
{
5757
$merge: {
5858
into: "genre_summary",
@@ -99,18 +99,18 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
9999
<Tabs groupId="aggregations">
100100
<TabItem value="books" label="through 'books' collection">
101101
```js
102-
db.books.aggregate([
103-
{ $unwind: "$authors" },
104-
{ $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } },
105-
{
106-
$merge: {
107-
into: "author_stats",
108-
on: "_id",
109-
whenMatched: "merge",
110-
whenNotMatched: "insert",
111-
},
102+
db.books.aggregate([
103+
{ $unwind: "$authors" },
104+
{ $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } },
105+
{
106+
$merge: {
107+
into: "author_stats",
108+
on: "_id",
109+
whenMatched: "merge",
110+
whenNotMatched: "insert",
112111
},
113-
]);
112+
},
113+
]);
114114
```
115115
</TabItem>
116116

docusaurus.config.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,20 @@ const footerLinks = [
5656
},
5757
{
5858
label: "Forums",
59-
href: `https://www.mongodb.com/community/forums/${utmParams}`,
59+
href: `https://www.mongodb.com/community/forums/?${utmParams}`,
6060
},
6161
{
6262
label: "Developer Center",
63-
href: `https://www.mongodb.com/developer/${utmParams}`,
63+
href: `https://www.mongodb.com/developer/?${utmParams}`,
6464
},
6565
{
6666
label: "MongoDB University",
67-
href: `https://learn.mongodb.com/${utmParams}`,
67+
href: `https://learn.mongodb.com/?${utmParams}`,
6868
},
6969
{
7070
href: `https://github.com/${organizationName}/${workshopName}`,
7171
label: "This lab in GitHub",
7272
},
73-
{
74-
label: ${new Date().getFullYear()} MongoDB, Inc.`,
75-
href: "#",
76-
},
7773
];
7874

7975
///////////////////////////////////////////////////////////////////////////////
@@ -156,6 +152,7 @@ const config = {
156152
footer: {
157153
style: "dark",
158154
links: footerLinks,
155+
copyright: ${new Date().getFullYear()} MongoDB, Inc.`,
159156
},
160157
prism: {
161158
theme: lightCodeTheme,

0 commit comments

Comments
 (0)