@@ -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 >
0 commit comments