Skip to content

Commit b5be334

Browse files
refactor: improve group challenges and add clearer aggregation hints
1 parent d5c972a commit b5be334

1 file changed

Lines changed: 49 additions & 8 deletions

File tree

java/104_aggregation_pipeline_group_by.ipynb

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"\n",
4848
"import static com.mongodb.client.model.Sorts.descending;\n",
4949
"import static com.mongodb.client.model.Aggregates.limit;\n",
50+
"import static com.mongodb.client.model.Aggregates.group;\n",
51+
"import static com.mongodb.client.model.Aggregates.sort;\n",
5052
"\n",
5153
"import org.bson.Document;\n",
5254
"import java.util.Arrays;\n",
@@ -69,7 +71,8 @@
6971
"}\n",
7072
"\n",
7173
"MongoDatabase library = mongoClient.getDatabase(\"library\");\n",
72-
"MongoCollection<Document> books = library.getCollection(\"books\");"
74+
"MongoCollection<Document> books = library.getCollection(\"books\");\n",
75+
"MongoCollection<Document> reviews = library.getCollection(\"reviews\");\n"
7376
]
7477
},
7578
{
@@ -118,7 +121,10 @@
118121
"id": "d96043ab",
119122
"metadata": {},
120123
"source": [
121-
"### 1. Find the average book rating of all books\n",
124+
"### 1. Find the average rating for each book\n",
125+
"\n",
126+
"Hint: use the `reviews` collection, group by `bookId`, and calculate the average of the `rating` field.\n",
127+
"\n",
122128
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/group#-1-find-the-average-book-rating-of-all-books)"
123129
]
124130
},
@@ -129,15 +135,27 @@
129135
"metadata": {},
130136
"outputs": [],
131137
"source": [
132-
"// type your code here"
138+
"// TYPE YOUR CODE HERE\n",
139+
"\n",
140+
"AggregateIterable<Document> result = reviews.aggregate(\n",
141+
" List.of(\n",
142+
" <REPLACE_WITH_GROUP_STAGE>\n",
143+
" )\n",
144+
");\n",
145+
"\n",
146+
"for (Document doc : result) {\n",
147+
" System.out.println(\"result: \" + doc.toJson());\n",
148+
"}"
133149
]
134150
},
135151
{
136152
"cell_type": "markdown",
137153
"id": "2b9bb53e",
138154
"metadata": {},
139155
"source": [
140-
"### 2. Find users with the most number of reviews (Hint: use sum + sort)\n",
156+
"### 2. Find the top 5 users with the highest number of reviews - use (Sort + Sum)\n",
157+
"Hint: use `sum` + `sort` to count and order the results, and don’t forget to use `limit` to keep the output easier to inspect.\n",
158+
"\n",
141159
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/group#-2-find-users-with-the-most-number-of-reviews-hint-use-the-name-field-in-the-reviews-collection)"
142160
]
143161
},
@@ -148,16 +166,28 @@
148166
"metadata": {},
149167
"outputs": [],
150168
"source": [
151-
"// type your code here"
169+
"AggregateIterable<Document> result = reviews.aggregate(\n",
170+
" List.of(\n",
171+
" <REPLACE_WITH_GROUP_STAGE> \n",
172+
" <REPLACE_WITH_LIMIT_STAGE>\n",
173+
" )\n",
174+
");\n",
175+
"\n",
176+
"for (Document doc : result) {\n",
177+
" System.out.println(\"user: \" + doc.toJson());\n",
178+
"}"
152179
]
153180
},
154181
{
155182
"cell_type": "markdown",
156183
"id": "74e33d58",
157184
"metadata": {},
158185
"source": [
159-
"### 3. Find users with the most number of reviews (Hint: use $sortByCount)\n",
160-
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/group#-2-find-users-with-the-most-number-of-reviews-hint-use-the-name-field-in-the-reviews-collection)"
186+
"### 3. Find the top 5 users with the highest number of reviews - use (SortByCount)\n",
187+
"\n",
188+
"Hint: use `sortByCount` with the `name` field, and remember to use `$limit` to keep the output easier to inspect.\n",
189+
"\n",
190+
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/group#-2-find-users-with-the-most-number-of-reviews-hint-use-the-name-field-in-the-reviews-collection)\n"
161191
]
162192
},
163193
{
@@ -167,7 +197,18 @@
167197
"metadata": {},
168198
"outputs": [],
169199
"source": [
170-
"// type your code here"
200+
"// TYPE YOUR CODE HERE\n",
201+
"\n",
202+
"AggregateIterable<Document> result = reviews.aggregate(\n",
203+
" List.of(\n",
204+
" <REPLACE_WITH_SORT_BY_COUNT_STAGE>,\n",
205+
" <REPLACE_WITH_LIMIT_STAGE>\n",
206+
" )\n",
207+
");\n",
208+
"\n",
209+
"for (Document doc : result) {\n",
210+
" System.out.println(\"user: \" + doc.toJson());\n",
211+
"}"
171212
]
172213
}
173214
],

0 commit comments

Comments
 (0)