From 2aa9bf6552c4223e4a9c64b1064f1c26c65c83fc Mon Sep 17 00:00:00 2001 From: Bohyun Jung Date: Mon, 16 Jun 2025 17:13:59 +0900 Subject: [PATCH 1/3] Udate docusaurus config - mongodb-developer/docusaurus-workshop#11 --- docusaurus.config.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 5ca6050..b31664f 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -56,24 +56,20 @@ const footerLinks = [ }, { label: "Forums", - href: `https://www.mongodb.com/community/forums/${utmParams}`, + href: `https://www.mongodb.com/community/forums/?${utmParams}`, }, { label: "Developer Center", - href: `https://www.mongodb.com/developer/${utmParams}`, + href: `https://www.mongodb.com/developer/?${utmParams}`, }, { label: "MongoDB University", - href: `https://learn.mongodb.com/${utmParams}`, + href: `https://learn.mongodb.com/?${utmParams}`, }, { href: `https://github.com/${organizationName}/${workshopName}`, label: "This lab in GitHub", }, - { - label: `© ${new Date().getFullYear()} MongoDB, Inc.`, - href: "#", - }, ]; /////////////////////////////////////////////////////////////////////////////// @@ -156,6 +152,7 @@ const config = { footer: { style: "dark", links: footerLinks, + copyright: `© ${new Date().getFullYear()} MongoDB, Inc.`, }, prism: { theme: lightCodeTheme, From 9e4e76f2b113fe0f614b2340c2a7db5ba1bbb499 Mon Sep 17 00:00:00 2001 From: Bohyun Jung Date: Mon, 16 Jun 2025 17:28:17 +0900 Subject: [PATCH 2/3] Update the field names in the example and challenge queries - Includes suggested change in mongodb-developer/sql-to-query-api-lab#16 --- docs/40-CRUD/1-WHERE.mdx | 2 +- docs/40-CRUD/2-SELECT.mdx | 4 ++-- docs/40-CRUD/3-ORDER-LIMIT.mdx | 2 +- docs/50-aggregation/4-group.mdx | 4 ++-- docs/50-aggregation/5-lookup.mdx | 12 +++++------ docs/50-aggregation/7-merge.mdx | 34 ++++++++++++++++---------------- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx index 939a143..d410ae8 100644 --- a/docs/40-CRUD/1-WHERE.mdx +++ b/docs/40-CRUD/1-WHERE.mdx @@ -115,7 +115,7 @@ Now, translate the following into a MongoDB query. Answer
```js - db.books.find({ genres: "Science", pages: {$gt: 300} }); + db.books.find({ "genre.name": "Science", pages: {$gt: 300} }); ```
diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index b6743d3..5f7d24c 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -55,7 +55,7 @@ Here: ## **Example 3: Using projection along with a query** ```js -db.books.find({ genres: "Science" }, { title: 1, totalInventory: 1, _id: 0 }); +db.books.find({ "genre.name": "Science" }, { title: 1, totalInventory: 1, _id: 0 }); ``` **Equivalent SQL query:** @@ -90,7 +90,7 @@ Here: Answer
```js - db.books.find({genres: "History"}, {_id: 0, authors: 0}); + db.books.find({ "genre.name": "History" }, { _id: 0, authors: 0 }); ```
diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index 294536c..06b7a56 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -37,7 +37,7 @@ This fetches the **5 books with the highest stock**. ```js db.books - .find({ genres: "Fiction" }, { title: 1, pages: 1 }) + .find({ "genre.name": "Fiction" }, { title: 1, pages: 1 }) .sort({ pages: -1 }) .limit(10); ``` diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index 33b733e..a316d85 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -137,10 +137,10 @@ GROUP BY year; db.reviews.aggregate([ { $group: { - _id: "$bookId", + _id: "$_id.bookId", avgRating: { $avg: "$rating" } } - }, + } ]); ``` diff --git a/docs/50-aggregation/5-lookup.mdx b/docs/50-aggregation/5-lookup.mdx index 7bd4687..ee5b04a 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -99,12 +99,12 @@ The $lookup operation creates an array within each book document. Using $unwind db.books.aggregate([ { $lookup: - { - from: "reviews", - localField: "_id", - foreignField: "bookId", - as: "reviews" - } + { + from: "reviews", + localField: "_id", + foreignField: "_id.bookId", + as: "reviews" + } } ]); ``` diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index e6175dd..8fa2bf5 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -13,10 +13,10 @@ The `$merge` stage enables you to store aggregation results into a different col ### **Key features:** -✔️ Inserts new documents if they don’t exist -✔️ Updates existing documents based on `_id` or a specified field -✔️ Can replace, merge, or discard duplicate records -✔️ Useful for **ETL workflows, reporting tables, and maintaining summary data** +- ✔️ Inserts new documents if they don’t exist +- ✔️ Updates existing documents based on `_id` or a specified field +- ✔️ Can replace, merge, or discard duplicate records +- ✔️ Useful for **ETL workflows, reporting tables, and maintaining summary data** --- @@ -51,8 +51,8 @@ The `$merge` stage enables you to store aggregation results into a different col ```js db.books.aggregate([ - { $unwind: "$genres" }, - { $group: { _id: "$genres", totalBooks: { $sum: 1 } } }, + { $unwind: "$genre" }, + { $group: { _id: "$genre.genreId", totalBooks: { $sum: 1 } } }, { $merge: { into: "genre_summary", @@ -99,18 +99,18 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks); ```js - db.books.aggregate([ - { $unwind: "$authors" }, - { $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } }, - { - $merge: { - into: "author_stats", - on: "_id", - whenMatched: "merge", - whenNotMatched: "insert", - }, + db.books.aggregate([ + { $unwind: "$authors" }, + { $group: { _id: "$authors.name", totalBooks: { $sum: 1 } } }, + { + $merge: { + into: "author_stats", + on: "_id", + whenMatched: "merge", + whenNotMatched: "insert", }, - ]); + }, + ]); ``` From 3c4e1d99c1d7a224427f9bc8774833c9943d9035 Mon Sep 17 00:00:00 2001 From: Bohyun Jung Date: Mon, 16 Jun 2025 20:37:45 +0900 Subject: [PATCH 3/3] Update sort and limit section - Change challenge question to year 2000 for more significant sort - Check if authors field exists before getting the size --- docs/50-aggregation/3-sort-limit.mdx | 61 ++++++++++++++++------------ 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/docs/50-aggregation/3-sort-limit.mdx b/docs/50-aggregation/3-sort-limit.mdx index ae0564d..a92c31a 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -72,7 +72,7 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5; ## 👐 Challenge -### 👐 1. After the year 2010, which book has the most number of authors? +### 👐 1. After the year 2000, which book has the most number of authors?
Answer @@ -89,44 +89,51 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre ```js db.books.aggregate([ - { - $match: { year: {$gt: 2010}} - }, - { - $project: { - title: 1, - authors: 1, - numAuthors: {$size: "$authors"}, - _id: 0 - } - }, - { - $sort: { "numAuthors": -1} - }, - { - $limit: 1 - } - ]); - ``` - - - ```js - db.books.aggregate([ { - $match: { year: {$gt: 2010}} + $match: { year: { $gt: 2000 } } + }, + { + $match: { + authors: { $exists: true }, + } }, { $addFields: { - numAuthors: {$size: "$authors"}, + numAuthors: { $size: "$authors" }, } }, { - $sort: { "numAuthors": -1} + $sort: { "numAuthors": -1 } }, { $limit: 1 } ]); + ``` + + + ```js + db.books.aggregate([ + { + $match: { year: { $gt: 2000 } } + }, + { + $match: { + authors: { $exists: true }, + } + }, + { + $addFields: { + numAuthors: { $size: "$authors" }, + } + }, + { + $sort: { "numAuthors": -1 } + }, + { + $limit: 1 + } + ]); ```