Skip to content

Commit 4c2bea4

Browse files
Copilotbohyunjung
authored andcommitted
Update $group challenge #2 to find top 5 reviewers with $limit
Changed the question from 'Find users with the most number of reviews' to 'Find the top 5 users with the most number of reviews' and added $limit: 5 to all code examples across all languages (JavaScript, C#, Python, Java) and both flavors ($group+$sort and $sortByCount).
1 parent 9cfad6d commit 4c2bea4

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

docs/50-aggregation/4-group.mdx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

0 commit comments

Comments
 (0)