Skip to content

Commit dd4c1ee

Browse files
Merge pull request #4 from mongodb-developer/feature/code-improvements
Feature/code improvements
2 parents 2389bbd + b5be334 commit dd4c1ee

6 files changed

Lines changed: 253 additions & 70 deletions

java/100_aggregation_pipeline_match.ipynb

Lines changed: 99 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,39 @@
3737
"outputs": [],
3838
"source": [
3939
"// Import the MongoDB Driver using Maven\n",
40-
"%maven org.mongodb:mongodb-driver-sync:5.0.0\n",
41-
" \n",
40+
"%maven org.mongodb:mongodb-driver-sync:5.5.1\n",
41+
"%maven org.slf4j:slf4j-simple:1.7.36\n",
42+
"\n",
4243
"import com.mongodb.client.MongoClient;\n",
4344
"import com.mongodb.client.MongoClients;\n",
4445
"import com.mongodb.client.MongoDatabase;\n",
4546
"import com.mongodb.client.MongoCollection;\n",
4647
"import com.mongodb.client.AggregateIterable;\n",
47-
"\n",
48+
"import com.mongodb.client.model.Aggregates;\n",
4849
"import com.mongodb.client.model.Projections;\n",
50+
"\n",
4951
"import static com.mongodb.client.model.Filters.gt;\n",
52+
"import static com.mongodb.client.model.Filters.eq;\n",
53+
"import static com.mongodb.client.model.Projections.include;\n",
54+
"import static com.mongodb.client.model.Projections.excludeId;\n",
55+
"import static com.mongodb.client.model.Projections.fields;\n",
5056
"\n",
51-
"import com.mongodb.client.model.Aggregates;\n",
57+
"import static com.mongodb.client.model.Aggregates.project;\n",
5258
"import static com.mongodb.client.model.Aggregates.match;\n",
5359
"import static com.mongodb.client.model.Aggregates.limit;\n",
5460
"import static com.mongodb.client.model.Aggregates.sort;\n",
55-
"import static com.mongodb.client.model.Sorts.ascending;\n",
61+
"import static com.mongodb.client.model.Sorts.descending;\n",
5662
"\n",
5763
"import org.bson.Document;\n",
5864
"import org.bson.conversions.Bson;\n",
5965
"\n",
6066
"import org.bson.Document;\n",
6167
"import org.bson.conversions.Bson;\n",
6268
"\n",
69+
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
70+
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
71+
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
72+
"\n",
6373
"// Set your connection String\n",
6474
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
6575
"\n",
@@ -83,7 +93,7 @@
8393
"source": [
8494
"## $match\n",
8595
"\n",
86-
"We'll get all books written after 2010."
96+
"Well use $match to return only the book with the title \"Treasure of the Sun\"."
8797
]
8898
},
8999
{
@@ -97,9 +107,8 @@
97107
},
98108
"outputs": [],
99109
"source": [
100-
"Bson yearFilter = gt(\"year\", 2010);\n",
101-
"Bson matchStage = match(yearFilter);\n",
102-
"\n",
110+
"Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n",
111+
"Bson matchStage = match(titleFilter);\n",
103112
"List<Bson> aggregationPipeline = List.of(matchStage);\n",
104113
"\n",
105114
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
@@ -110,14 +119,55 @@
110119
"}"
111120
]
112121
},
122+
{
123+
"cell_type": "markdown",
124+
"id": "74f5fd5a",
125+
"metadata": {},
126+
"source": [
127+
"## $project\n",
128+
"\n",
129+
"We’ll project only the `title` and `year` fields for the book \"Treasure of the Sun\", excluding the `_id` field from the result."
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"id": "50b8aa38",
136+
"metadata": {
137+
"vscode": {
138+
"languageId": "java"
139+
}
140+
},
141+
"outputs": [],
142+
"source": [
143+
"Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n",
144+
"Bson matchStage = match(titleFilter);\n",
145+
"\n",
146+
"Bson projectStage = project(\n",
147+
" fields(\n",
148+
" include(\"title\", \"year\"),\n",
149+
" excludeId()\n",
150+
" )\n",
151+
");\n",
152+
"\n",
153+
"List<Bson> aggregationPipeline = List.of(matchStage, projectStage);\n",
154+
"\n",
155+
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
156+
"\n",
157+
"// Iterate through the results\n",
158+
"for (Document doc : result) {\n",
159+
" System.out.println(\"book: \" + doc.toJson());\n",
160+
"}\n"
161+
]
162+
},
113163
{
114164
"cell_type": "markdown",
115165
"id": "9f6caab3",
116166
"metadata": {},
117167
"source": [
118-
"### $limit\n",
168+
"## $limit\n",
119169
"\n",
120-
"We'll get books published after 2010 and limit the results to 10 documents."
170+
"Well first match books published after 2010, then use `$limit` to return only 3 documents."
121171
]
122172
},
123173
{
@@ -133,12 +183,13 @@
133183
"source": [
134184
"Bson yearFilter = gt(\"year\", 2010);\n",
135185
"Bson matchStage = match(yearFilter);\n",
136-
"Bson limitStage = limit(10);\n",
186+
"Bson limitStage = limit(3);\n",
137187
"\n",
138188
"List<Bson> aggregationPipeline = List.of(matchStage, limitStage);\n",
139189
"\n",
140190
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
141191
"\n",
192+
"// Iterate through the results\n",
142193
"for (Document doc : result) {\n",
143194
" System.out.println(\"book: \" + doc.toJson());\n",
144195
"}"
@@ -155,9 +206,9 @@
155206
"id": "worth-windows",
156207
"metadata": {},
157208
"source": [
158-
"### $sort\n",
209+
"## $sort\n",
159210
"\n",
160-
"We'll sort the results by publication year in ascending order."
211+
"Well sort the books by the number of pages in descending order, limit the result to 3 documents, and project only the title and pages fields."
161212
]
162213
},
163214
{
@@ -171,19 +222,25 @@
171222
},
172223
"outputs": [],
173224
"source": [
174-
"Bson yearFilter = gt(\"year\", 2010);\n",
175-
"Bson matchStage = match(yearFilter);\n",
176-
"Bson limitStage = limit(10);\n",
177-
"Bson sortStage = sort(ascending(\"year\"));\n",
225+
"Bson projectStage = project(\n",
226+
" fields(\n",
227+
" include(\"title\", \"pages\"),\n",
228+
" excludeId()\n",
229+
" )\n",
230+
");\n",
231+
"\n",
232+
"Bson sortStage = sort(descending(\"pages\"));\n",
233+
"Bson limitStage = limit(3);\n",
178234
"\n",
179235
"List<Bson> aggregationPipeline = List.of(\n",
180-
" matchStage,\n",
181-
" limitStage,\n",
182-
" sortStage\n",
236+
" projectStage,\n",
237+
" sortStage,\n",
238+
" limitStage\n",
183239
");\n",
184240
"\n",
185241
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
186242
"\n",
243+
"// Iterate through the results\n",
187244
"for (Document doc : result) {\n",
188245
" System.out.println(\"book: \" + doc.toJson());\n",
189246
"}"
@@ -218,7 +275,15 @@
218275
},
219276
"outputs": [],
220277
"source": [
221-
"// type your code here"
278+
"// TYPE YOUR CODE HERE\n",
279+
"\n",
280+
"AggregateIterable<Document> result = books.aggregate(\n",
281+
" List.of(<REPLACE_WITH_MATCH_STAGE>) \n",
282+
");\n",
283+
"\n",
284+
"for (Document doc : result) {\n",
285+
" System.out.println(\"book: \" + doc.toJson());\n",
286+
"}"
222287
]
223288
},
224289
{
@@ -242,7 +307,18 @@
242307
},
243308
"outputs": [],
244309
"source": [
245-
"// type your code here"
310+
"// TYPE YOUR CODE HERE\n",
311+
"\n",
312+
"AggregateIterable<Document> result = books.aggregate(\n",
313+
" List.of(\n",
314+
" <REPLACE_WITH_MATCH_STAGE>,\n",
315+
" <REPLACE_WITH_PROJECT_STAGE>\n",
316+
" )\n",
317+
");\n",
318+
"\n",
319+
"for (Document doc : result) {\n",
320+
" System.out.println(\"book: \" + doc.toJson());\n",
321+
"}"
246322
]
247323
}
248324
],

java/101_aggregation_pipeline_arrays.ipynb

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
"outputs": [],
3838
"source": [
3939
"// Import the MongoDB Driver using Maven\n",
40-
"%maven org.mongodb:mongodb-driver-sync:5.0.0\n",
41-
" \n",
40+
"%maven org.mongodb:mongodb-driver-sync:5.5.1\n",
41+
"%maven org.slf4j:slf4j-simple:1.7.36\n",
42+
"\n",
4243
"import com.mongodb.client.MongoClient;\n",
4344
"import com.mongodb.client.MongoClients;\n",
4445
"import com.mongodb.client.MongoDatabase;\n",
@@ -55,6 +56,9 @@
5556
"import static com.mongodb.client.model.Filters.in;\n",
5657
"import static com.mongodb.client.model.Sorts.descending;\n",
5758
"\n",
59+
"import static com.mongodb.client.model.Projections.include;\n",
60+
"import static com.mongodb.client.model.Projections.excludeId;\n",
61+
"import static com.mongodb.client.model.Projections.fields;\n",
5862
"\n",
5963
"import static com.mongodb.client.model.Aggregates.match;\n",
6064
"import static com.mongodb.client.model.Aggregates.project;\n",
@@ -64,6 +68,10 @@
6468
"import org.bson.Document;\n",
6569
"import org.bson.conversions.Bson;\n",
6670
"\n",
71+
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
72+
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
73+
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
74+
"\n",
6775
"// Set your connection String\n",
6876
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
6977
"\n",
@@ -93,12 +101,12 @@
93101
"id": "handled-symbol",
94102
"metadata": {},
95103
"source": [
96-
"### $match: $all\n",
104+
"### $match with the `$all` operator\n",
97105
"\n",
98106
"[arrays-reference](\n",
99107
"https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/using-arrays/simple-match-array)\n",
100108
"\n",
101-
"If you want to find books whose `genres` array contains both \"Family Life\" and \"Fiction\", in any order, use:"
109+
"If you want to find books whose `genres` array contains both `\"Family Life\"` and `\"Fiction\"`, in any order, use:"
102110
]
103111
},
104112
{
@@ -112,13 +120,17 @@
112120
},
113121
"outputs": [],
114122
"source": [
115-
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
116-
" Aggregates.match(Filters.all(\"genres\", \"Family Life\", \"Fiction\")),\n",
117-
" Aggregates.project(new Document()\n",
118-
" .append(\"title\", 1)\n",
119-
" .append(\"genres\", 1)\n",
123+
"AggregateIterable<Document> result = books.aggregate(\n",
124+
" List.of(\n",
125+
" match(all(\"genres\", \"Family Life\", \"Fiction\")),\n",
126+
" project(\n",
127+
" fields(\n",
128+
" include(\"title\", \"genres\"),\n",
129+
" excludeId()\n",
130+
" )\n",
131+
" )\n",
120132
" )\n",
121-
"));\n",
133+
");\n",
122134
"\n",
123135
"// Iterate through the results\n",
124136
"for (Document doc : result) {\n",
@@ -131,7 +143,7 @@
131143
"id": "9f6caab3",
132144
"metadata": {},
133145
"source": [
134-
"### $match: $in\n",
146+
"### $match with the `$in` operator\n",
135147
"\n",
136148
"Use `$in` to find books where the `genres` array contains at least one of the specified values."
137149
]
@@ -147,13 +159,17 @@
147159
},
148160
"outputs": [],
149161
"source": [
150-
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
151-
" Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n",
152-
" Aggregates.project(new Document()\n",
153-
" .append(\"title\", 1)\n",
154-
" .append(\"genres\", 1)\n",
162+
"// Note: `Document` can also be used here because it implements `Bson`.\n",
163+
"\n",
164+
"AggregateIterable<Document> result = books.aggregate(\n",
165+
" List.of(\n",
166+
" Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n",
167+
" Aggregates.project(new Document()\n",
168+
" .append(\"title\", 1)\n",
169+
" .append(\"genres\", 1)\n",
170+
" )\n",
155171
" )\n",
156-
"));\n",
172+
");\n",
157173
"\n",
158174
"// Iterate through the results\n",
159175
"for (Document doc : result) {\n",

java/102_aggregation_pipeline_unwind.ipynb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
"outputs": [],
3838
"source": [
3939
"// Import the MongoDB Driver using Maven\n",
40-
"%maven org.mongodb:mongodb-driver-sync:5.0.0\n",
41-
" \n",
40+
"%maven org.mongodb:mongodb-driver-sync:5.5.1\n",
41+
"%maven org.slf4j:slf4j-simple:1.7.36\n",
42+
"\n",
4243
"import com.mongodb.client.AggregateIterable;\n",
4344
"import com.mongodb.client.MongoClient;\n",
4445
"import com.mongodb.client.MongoClients;\n",
@@ -48,6 +49,19 @@
4849
"import com.mongodb.client.model.Filters;\n",
4950
"import org.bson.Document;\n",
5051
"\n",
52+
"import static com.mongodb.client.model.Filters.eq;\n",
53+
"import static com.mongodb.client.model.Projections.include;\n",
54+
"import static com.mongodb.client.model.Projections.excludeId;\n",
55+
"import static com.mongodb.client.model.Projections.fields;\n",
56+
"\n",
57+
"import static com.mongodb.client.model.Aggregates.match;\n",
58+
"import static com.mongodb.client.model.Aggregates.project;\n",
59+
"import static com.mongodb.client.model.Aggregates.unwind;\n",
60+
"\n",
61+
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
62+
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
63+
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
64+
"\n",
5165
"// Set your connection String\n",
5266
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
5367
"\n",
@@ -77,7 +91,7 @@
7791
"source": [
7892
"## $unwind\n",
7993
"\n",
80-
"This pipeline first selects the book with `_id` `\"0004127382\"` and then uses `$unwind` to split the `attributes` array into separate documents. Each result contains the book title and one individual attribute.\n"
94+
"This pipeline first selects the book with `_id` `\"0004127382\"`, then uses `$unwind` to split the `attributes` array into separate documents, and finally uses `$project` to return only the `title` and `attributes` fields."
8195
]
8296
},
8397
{
@@ -91,14 +105,13 @@
91105
},
92106
"outputs": [],
93107
"source": [
94-
"AggregateIterable<Document> result = books.aggregate(List.of(\n",
95-
" Aggregates.match(Filters.eq(\"_id\", \"0004127382\")),\n",
96-
" Aggregates.unwind(\"$attributes\"), \n",
97-
" Aggregates.project(new Document()\n",
98-
" .append(\"title\", 1)\n",
99-
" .append(\"attributes\", 1)\n",
108+
"AggregateIterable<Document> result = books.aggregate(\n",
109+
" List.of(\n",
110+
" match(eq(\"_id\", \"0004127382\")),\n",
111+
" unwind(\"$attributes\"),\n",
112+
" project(fields(include(\"title\", \"attributes\"), excludeId()))\n",
100113
" )\n",
101-
"));\n",
114+
");\n",
102115
"\n",
103116
"// Iterate through the results\n",
104117
"for (Document doc : result) {\n",

0 commit comments

Comments
 (0)