Skip to content

Commit 9e4e76f

Browse files
committed
Update the field names in the example and challenge queries
- Includes suggested change in #16
1 parent 2aa9bf6 commit 9e4e76f

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
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/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

0 commit comments

Comments
 (0)