Skip to content

Commit 083a91f

Browse files
authored
Merge pull request #40 from mongodb-developer/copilot/fix-reviews-collection-id-reference
Fix incorrect bookId field path and add $limit to $group challenge
2 parents 4279187 + be41b13 commit 083a91f

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

docs/50-aggregation/4-group.mdx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ GROUP BY year;
140140
await reviews.aggregate([
141141
{
142142
$group: {
143-
_id: "$_id.bookId",
143+
_id: "$bookId",
144144
avgRating: { $avg: "$rating" }
145145
}
146146
}
@@ -207,7 +207,7 @@ GROUP BY year;
207207
</Tabs>
208208
</details>
209209

210-
### 👐 2. Find users with the most number of reviews (Hint: use the `name` field in the reviews collection)
210+
### 👐 2. Find the top 5 users with the most number of reviews (Hint: use the `name` field in the reviews collection)
211211

212212
<details>
213213
<summary>Answer</summary>
@@ -233,6 +233,9 @@ GROUP BY year;
233233
totalReviews: -1,
234234
},
235235
},
236+
{
237+
$limit: 5,
238+
},
236239
]).toArray();
237240
```
238241
</div>
@@ -245,6 +248,9 @@ GROUP BY year;
245248
{
246249
$sortByCount: "$name",
247250
},
251+
{
252+
$limit: 5,
253+
},
248254
]).toArray();
249255
```
250256
</div>
@@ -275,7 +281,8 @@ GROUP BY year;
275281
TotalReviews = g.Count()
276282
}
277283
)
278-
.SortByDescending(r => r.TotalReviews);
284+
.SortByDescending(r => r.TotalReviews)
285+
.Limit(5);
279286

280287
var result = await pipeline.ToListAsync();
281288
```
@@ -288,7 +295,8 @@ GROUP BY year;
288295
var pipeline = reviewsCollection.Aggregate()
289296
.AppendStage<BsonDocument>(
290297
new BsonDocument("$sortByCount", "$name")
291-
);
298+
)
299+
.Limit(5);
292300

293301
var result = pipeline.ToList();
294302
```
@@ -310,6 +318,9 @@ GROUP BY year;
310318
},
311319
{
312320
"$sort": {"totalReviews": -1}
321+
},
322+
{
323+
"$limit": 5
313324
}
314325
])
315326

@@ -325,6 +336,9 @@ GROUP BY year;
325336
reviewers_count = reviews.aggregate([
326337
{
327338
"$sortByCount": "$name"
339+
},
340+
{
341+
"$limit": 5
328342
}
329343
])
330344

@@ -346,7 +360,8 @@ GROUP BY year;
346360
List.of(Aggregates.group(
347361
"$name",
348362
Accumulators.sum("totalReviews", 1)),
349-
Aggregates.sort(descending("totalReviews")))
363+
Aggregates.sort(descending("totalReviews")),
364+
Aggregates.limit(5))
350365
).forEach(r -> System.out.println(r.toJson()));
351366
```
352367
</div>
@@ -359,7 +374,8 @@ GROUP BY year;
359374

360375
reviews.aggregate(
361376
List.of(
362-
Aggregates.sortByCount("$name"))
377+
Aggregates.sortByCount("$name"),
378+
Aggregates.limit(5))
363379
).forEach(r -> System.out.println(r.toJson()));
364380
```
365381
</div>

docs/50-aggregation/5-lookup.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ The $lookup operation creates an array within each book document. Using $unwind
108108
{
109109
from: "reviews",
110110
localField: "_id",
111-
foreignField: "_id.bookId",
111+
foreignField: "bookId",
112112
as: "reviews"
113113
}
114114
}
@@ -125,7 +125,7 @@ The $lookup operation creates an array within each book document. Using $unwind
125125
{
126126
from: "reviews",
127127
localField: "_id",
128-
foreignField: "_id.bookId",
128+
foreignField: "bookId",
129129
as: "reviews"
130130
}
131131
}
@@ -157,7 +157,7 @@ The $lookup operation creates an array within each book document. Using $unwind
157157
{
158158
"from": "reviews",
159159
"localField": "_id",
160-
"foreignField": "_id.bookId",
160+
"foreignField": "bookId",
161161
"as": "reviews"
162162
}
163163
}

0 commit comments

Comments
 (0)