|
47 | 47 | "\n", |
48 | 48 | "import static com.mongodb.client.model.Sorts.descending;\n", |
49 | 49 | "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", |
50 | 52 | "\n", |
51 | 53 | "import org.bson.Document;\n", |
52 | 54 | "import java.util.Arrays;\n", |
|
69 | 71 | "}\n", |
70 | 72 | "\n", |
71 | 73 | "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" |
73 | 76 | ] |
74 | 77 | }, |
75 | 78 | { |
|
118 | 121 | "id": "d96043ab", |
119 | 122 | "metadata": {}, |
120 | 123 | "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", |
122 | 128 | "[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/group#-1-find-the-average-book-rating-of-all-books)" |
123 | 129 | ] |
124 | 130 | }, |
|
129 | 135 | "metadata": {}, |
130 | 136 | "outputs": [], |
131 | 137 | "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 | + "}" |
133 | 149 | ] |
134 | 150 | }, |
135 | 151 | { |
136 | 152 | "cell_type": "markdown", |
137 | 153 | "id": "2b9bb53e", |
138 | 154 | "metadata": {}, |
139 | 155 | "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", |
141 | 159 | "[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)" |
142 | 160 | ] |
143 | 161 | }, |
|
148 | 166 | "metadata": {}, |
149 | 167 | "outputs": [], |
150 | 168 | "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 | + "}" |
152 | 179 | ] |
153 | 180 | }, |
154 | 181 | { |
155 | 182 | "cell_type": "markdown", |
156 | 183 | "id": "74e33d58", |
157 | 184 | "metadata": {}, |
158 | 185 | "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" |
161 | 191 | ] |
162 | 192 | }, |
163 | 193 | { |
|
167 | 197 | "metadata": {}, |
168 | 198 | "outputs": [], |
169 | 199 | "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 | + "}" |
171 | 212 | ] |
172 | 213 | } |
173 | 214 | ], |
|
0 commit comments