Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/40-CRUD/1-WHERE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Now, translate the following into a MongoDB query.
<summary>Answer</summary>
<div>
```js
db.books.find({ genres: "Science", pages: {$gt: 300} });
db.books.find({ "genre.name": "Science", pages: {$gt: 300} });
```
</div>
</details>
4 changes: 2 additions & 2 deletions docs/40-CRUD/2-SELECT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down Expand Up @@ -90,7 +90,7 @@ Here:
<summary>Answer</summary>
<div>
```js
db.books.find({genres: "History"}, {_id: 0, authors: 0});
db.books.find({ "genre.name": "History" }, { _id: 0, authors: 0 });
```
</div>
</details>
2 changes: 1 addition & 1 deletion docs/40-CRUD/3-ORDER-LIMIT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
Expand Down
61 changes: 34 additions & 27 deletions docs/50-aggregation/3-sort-limit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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?

<details>
<summary>Answer</summary>
Expand All @@ -89,44 +89,51 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
<TabItem value="mongodb-shell" label="Using $project">
```js
db.books.aggregate([
{
$match: { year: {$gt: 2010}}
},
{
$project: {
title: 1,
authors: 1,
numAuthors: {$size: "$authors"},
_id: 0
}
},
{
$sort: { "numAuthors": -1}
},
{
$limit: 1
}
]);
```
</TabItem>
<TabItem value="atlas" label="Using $addFields">
```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
}
]);
```
</TabItem>
<TabItem value="atlas" label="Using $addFields">
```js
db.books.aggregate([
{
$match: { year: { $gt: 2000 } }
},
{
$match: {
authors: { $exists: true },
}
},
{
$addFields: {
numAuthors: { $size: "$authors" },
}
},
{
$sort: { "numAuthors": -1 }
},
{
$limit: 1
}
]);
```
</TabItem>
</Tabs>
Expand Down
4 changes: 2 additions & 2 deletions docs/50-aggregation/4-group.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ GROUP BY year;
db.reviews.aggregate([
{
$group: {
_id: "$bookId",
_id: "$_id.bookId",
avgRating: { $avg: "$rating" }
}
},
}
]);
```

Expand Down
12 changes: 6 additions & 6 deletions docs/50-aggregation/5-lookup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]);
```
Expand Down
34 changes: 17 additions & 17 deletions docs/50-aggregation/7-merge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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**

---

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -99,18 +99,18 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
<Tabs groupId="aggregations">
<TabItem value="books" label="through 'books' collection">
```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",
},
]);
},
]);
```
</TabItem>

Expand Down
11 changes: 4 additions & 7 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "#",
},
];

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -156,6 +152,7 @@ const config = {
footer: {
style: "dark",
links: footerLinks,
copyright: `© ${new Date().getFullYear()} MongoDB, Inc.`,
},
prism: {
theme: lightCodeTheme,
Expand Down