Skip to content

Commit d601661

Browse files
Merge pull request #5 from mongodb-developer/fix/quick-fix
Fix/quick fix
2 parents dd4c1ee + fd3c4a2 commit d601661

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

java/101_aggregation_pipeline_arrays.ipynb

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
"import static com.mongodb.client.model.Aggregates.project;\n",
6565
"import static com.mongodb.client.model.Aggregates.sort;\n",
6666
"import static com.mongodb.client.model.Aggregates.addFields;\n",
67+
"import static com.mongodb.client.model.Aggregates.limit;\n",
68+
"\n",
6769
"\n",
6870
"import org.bson.Document;\n",
6971
"import org.bson.conversions.Bson;\n",
@@ -163,7 +165,7 @@
163165
"\n",
164166
"AggregateIterable<Document> result = books.aggregate(\n",
165167
" List.of(\n",
166-
" Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n",
168+
" Aggregates.match(in(\"genres\", \"Family Life\", \"Fiction\")),\n",
167169
" Aggregates.project(new Document()\n",
168170
" .append(\"title\", 1)\n",
169171
" .append(\"genres\", 1)\n",
@@ -205,7 +207,19 @@
205207
},
206208
"outputs": [],
207209
"source": [
208-
"// type your code here"
210+
"// TYPE YOUR CODE HERE\n",
211+
"\n",
212+
"AggregateIterable<Document> result = books.aggregate(\n",
213+
" List.of(\n",
214+
" <REPLACE_WITH_MATCH_STAGE>,\n",
215+
" <REPLACE_WITH_PROJECT_STAGE>\n",
216+
" )\n",
217+
");\n",
218+
"\n",
219+
"// Iterate through the results\n",
220+
"for (Document doc : result) {\n",
221+
" System.out.println(\"book: \" + doc.toJson());\n",
222+
"}"
209223
]
210224
},
211225
{
@@ -228,7 +242,19 @@
228242
},
229243
"outputs": [],
230244
"source": [
231-
"// type your code here"
245+
"// TYPE YOUR CODE HERE\n",
246+
"\n",
247+
"AggregateIterable<Document> result = books.aggregate(\n",
248+
" List.of(\n",
249+
" <REPLACE_WITH_MATCH_STAGE>,\n",
250+
" <REPLACE_WITH_ADDFIELDS_STAGE>\n",
251+
" )\n",
252+
");\n",
253+
"\n",
254+
"// Iterate through the results\n",
255+
"for (Document doc : result) {\n",
256+
" System.out.println(\"book: \" + doc.toJson());\n",
257+
"}"
232258
]
233259
}
234260
],

java/102_aggregation_pipeline_unwind.ipynb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"import com.mongodb.client.MongoDatabase;\n",
4848
"import com.mongodb.client.model.Aggregates;\n",
4949
"import com.mongodb.client.model.Filters;\n",
50+
"import com.mongodb.client.model.Projections;\n",
51+
"\n",
5052
"import org.bson.Document;\n",
5153
"\n",
5254
"import static com.mongodb.client.model.Filters.eq;\n",
@@ -118,6 +120,53 @@
118120
" System.out.println(\"book: \" + doc.toJson());\n",
119121
"}"
120122
]
123+
},
124+
{
125+
"cell_type": "markdown",
126+
"id": "df18e342",
127+
"metadata": {},
128+
"source": [
129+
"## Challenges"
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"id": "39c96bba",
135+
"metadata": {},
136+
"source": [
137+
"### Return one document per author for book id 1567189644\n",
138+
"\n",
139+
"Use `$match` to select the book with `_id` `\"1567189644\"`, then use `$unwind` to deconstruct the `authors` array. Finally, use `$project` to return only the book `title` and the author `name`.\n",
140+
"\n",
141+
"[Solution here](http://localhost:3000/sql-to-query-api-lab/docs/aggregation/group#-3-return-one-document-per-author-for-book-id-1567189644)\n"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": null,
147+
"id": "150a1497",
148+
"metadata": {
149+
"vscode": {
150+
"languageId": "java"
151+
}
152+
},
153+
"outputs": [],
154+
"source": [
155+
"//TYPE YOUR CODE HERE\n",
156+
"\n",
157+
"AggregateIterable<Document> result = books.aggregate(\n",
158+
" List.of(\n",
159+
" <REPLACE_WITH_MATCH_STAGE>,\n",
160+
" <REPLACE_WITH_UNWIND_STAGE>,\n",
161+
" <REPLACE_WITH_PROJECT_STAGE>,\n",
162+
" )\n",
163+
");\n",
164+
"\n",
165+
"// Iterate through the results\n",
166+
"for (Document doc : result) {\n",
167+
" System.out.println(\"book: \" + doc.toJson());\n",
168+
"}"
169+
]
121170
}
122171
],
123172
"metadata": {

java/104_aggregation_pipeline_group_by.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@
168168
"source": [
169169
"AggregateIterable<Document> result = reviews.aggregate(\n",
170170
" List.of(\n",
171-
" <REPLACE_WITH_GROUP_STAGE> \n",
171+
" <REPLACE_WITH_GROUP_STAGE>,\n",
172+
" <REPLACE_WITH_SORT_STAGE>,\n",
172173
" <REPLACE_WITH_LIMIT_STAGE>\n",
173174
" )\n",
174175
");\n",

0 commit comments

Comments
 (0)