From 469f70e28842005599698e2f1f0c7437a9191c3e Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Thu, 30 Apr 2026 16:38:21 -0300 Subject: [PATCH 01/15] Including sl4j import for removing unnecessary log --- java/100_aggregation_pipeline_match.ipynb | 1 + 1 file changed, 1 insertion(+) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 1090300..12bd104 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -38,6 +38,7 @@ "source": [ "// Import the MongoDB Driver using Maven\n", "%maven org.mongodb:mongodb-driver-sync:5.0.0\n", + "%maven org.slf4j:slf4j-simple:1.7.36\n", " \n", "import com.mongodb.client.MongoClient;\n", "import com.mongodb.client.MongoClients;\n", From d73c48b676a58279b65c2117fe9f23b72d29c12f Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 15:11:25 -0300 Subject: [PATCH 02/15] chore: upgrade MongoDB Java driver and remove unnecessary SLF4J logs --- java/100_aggregation_pipeline_match.ipynb | 19 ++++++++++++++----- java/101_aggregation_pipeline_arrays.ipynb | 10 ++++++++-- java/102_aggregation_pipeline_unwind.ipynb | 10 ++++++++-- java/103_aggregation_pipeline_lookup.ipynb | 10 ++++++++-- java/104_aggregation_pipeline_group_by.ipynb | 10 ++++++++-- java/105_aggregation_pipeline_merge.ipynb | 10 ++++++++-- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 12bd104..0b9a68e 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -37,19 +37,23 @@ "outputs": [], "source": [ "// Import the MongoDB Driver using Maven\n", - "%maven org.mongodb:mongodb-driver-sync:5.0.0\n", + "%maven org.mongodb:mongodb-driver-sync:5.5.1\n", "%maven org.slf4j:slf4j-simple:1.7.36\n", - " \n", + "\n", "import com.mongodb.client.MongoClient;\n", "import com.mongodb.client.MongoClients;\n", "import com.mongodb.client.MongoDatabase;\n", "import com.mongodb.client.MongoCollection;\n", "import com.mongodb.client.AggregateIterable;\n", - "\n", + "import com.mongodb.client.model.Aggregates;\n", "import com.mongodb.client.model.Projections;\n", + "\n", "import static com.mongodb.client.model.Filters.gt;\n", + "import static com.mongodb.client.model.Projections.include;\n", + "import static com.mongodb.client.model.Projections.excludeId;\n", + "import static com.mongodb.client.model.Projections.fields;\n", "\n", - "import com.mongodb.client.model.Aggregates;\n", + "import static com.mongodb.client.model.Aggregates.project;\n", "import static com.mongodb.client.model.Aggregates.match;\n", "import static com.mongodb.client.model.Aggregates.limit;\n", "import static com.mongodb.client.model.Aggregates.sort;\n", @@ -61,6 +65,11 @@ "import org.bson.Document;\n", "import org.bson.conversions.Bson;\n", "\n", + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", + "\n", + "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", + "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", + "\n", "// Set your connection String\n", "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", "\n", @@ -84,7 +93,7 @@ "source": [ "## $match\n", "\n", - "We'll get all books written after 2010." + "We’ll return all books published after 2010 using $match for filtering and $project to return only specific fields." ] }, { diff --git a/java/101_aggregation_pipeline_arrays.ipynb b/java/101_aggregation_pipeline_arrays.ipynb index e6f863e..3d43229 100644 --- a/java/101_aggregation_pipeline_arrays.ipynb +++ b/java/101_aggregation_pipeline_arrays.ipynb @@ -37,8 +37,9 @@ "outputs": [], "source": [ "// Import the MongoDB Driver using Maven\n", - "%maven org.mongodb:mongodb-driver-sync:5.0.0\n", - " \n", + "%maven org.mongodb:mongodb-driver-sync:5.5.1\n", + "%maven org.slf4j:slf4j-simple:1.7.36\n", + "\n", "import com.mongodb.client.MongoClient;\n", "import com.mongodb.client.MongoClients;\n", "import com.mongodb.client.MongoDatabase;\n", @@ -64,6 +65,11 @@ "import org.bson.Document;\n", "import org.bson.conversions.Bson;\n", "\n", + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", + "\n", + "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", + "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", + "\n", "// Set your connection String\n", "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", "\n", diff --git a/java/102_aggregation_pipeline_unwind.ipynb b/java/102_aggregation_pipeline_unwind.ipynb index 8f1d901..2772459 100644 --- a/java/102_aggregation_pipeline_unwind.ipynb +++ b/java/102_aggregation_pipeline_unwind.ipynb @@ -37,8 +37,9 @@ "outputs": [], "source": [ "// Import the MongoDB Driver using Maven\n", - "%maven org.mongodb:mongodb-driver-sync:5.0.0\n", - " \n", + "%maven org.mongodb:mongodb-driver-sync:5.5.1\n", + "%maven org.slf4j:slf4j-simple:1.7.36\n", + "\n", "import com.mongodb.client.AggregateIterable;\n", "import com.mongodb.client.MongoClient;\n", "import com.mongodb.client.MongoClients;\n", @@ -48,6 +49,11 @@ "import com.mongodb.client.model.Filters;\n", "import org.bson.Document;\n", "\n", + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", + "\n", + "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", + "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", + "\n", "// Set your connection String\n", "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", "\n", diff --git a/java/103_aggregation_pipeline_lookup.ipynb b/java/103_aggregation_pipeline_lookup.ipynb index d90aeca..b18aa21 100644 --- a/java/103_aggregation_pipeline_lookup.ipynb +++ b/java/103_aggregation_pipeline_lookup.ipynb @@ -37,8 +37,9 @@ "outputs": [], "source": [ "// Import the MongoDB Driver using Maven\n", - "%maven org.mongodb:mongodb-driver-sync:5.0.0\n", - " \n", + "%maven org.mongodb:mongodb-driver-sync:5.5.1\n", + "%maven org.slf4j:slf4j-simple:1.7.36\n", + "\n", "import com.mongodb.client.AggregateIterable;\n", "import com.mongodb.client.MongoClient;\n", "import com.mongodb.client.MongoClients;\n", @@ -53,6 +54,11 @@ "\n", "import org.bson.Document;\n", "\n", + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", + "\n", + "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", + "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", + "\n", "// Set your connection String\n", "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", "\n", diff --git a/java/104_aggregation_pipeline_group_by.ipynb b/java/104_aggregation_pipeline_group_by.ipynb index 6baabb1..586f590 100644 --- a/java/104_aggregation_pipeline_group_by.ipynb +++ b/java/104_aggregation_pipeline_group_by.ipynb @@ -33,8 +33,9 @@ "outputs": [], "source": [ "// Import the MongoDB Driver using Maven\n", - "%maven org.mongodb:mongodb-driver-sync:5.0.0\n", - " \n", + "%maven org.mongodb:mongodb-driver-sync:5.5.1\n", + "%maven org.slf4j:slf4j-simple:1.7.36\n", + "\n", "import com.mongodb.client.AggregateIterable;\n", "import com.mongodb.client.MongoClient;\n", "import com.mongodb.client.MongoClients;\n", @@ -49,6 +50,11 @@ "import org.bson.Document;\n", "import java.util.Arrays;\n", "\n", + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", + "\n", + "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", + "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", + "\n", "// Set your connection String\n", "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", "\n", diff --git a/java/105_aggregation_pipeline_merge.ipynb b/java/105_aggregation_pipeline_merge.ipynb index c3818ed..75f30a8 100644 --- a/java/105_aggregation_pipeline_merge.ipynb +++ b/java/105_aggregation_pipeline_merge.ipynb @@ -33,8 +33,9 @@ "outputs": [], "source": [ "// Import the MongoDB Driver using Maven\n", - "%maven org.mongodb:mongodb-driver-sync:5.0.0\n", - " \n", + "%maven org.mongodb:mongodb-driver-sync:5.5.1\n", + "%maven org.slf4j:slf4j-simple:1.7.36\n", + "\n", "import com.mongodb.client.AggregateIterable;\n", "import com.mongodb.client.MongoClient;\n", "import com.mongodb.client.MongoClients;\n", @@ -48,6 +49,11 @@ "\n", "import java.util.Arrays;\n", "\n", + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", + "\n", + "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", + "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", + "\n", "// Set your connection String\n", "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", "\n", From a865451b9e713176e63b16f4f313654772c30ad9 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 15:21:12 -0300 Subject: [PATCH 03/15] Simplify $match aggregation example using exact title filter --- java/100_aggregation_pipeline_match.ipynb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 0b9a68e..c57354c 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -49,6 +49,7 @@ "import com.mongodb.client.model.Projections;\n", "\n", "import static com.mongodb.client.model.Filters.gt;\n", + "import static com.mongodb.client.model.Filters.eq;\n", "import static com.mongodb.client.model.Projections.include;\n", "import static com.mongodb.client.model.Projections.excludeId;\n", "import static com.mongodb.client.model.Projections.fields;\n", @@ -93,7 +94,7 @@ "source": [ "## $match\n", "\n", - "We’ll return all books published after 2010 using $match for filtering and $project to return only specific fields." + "We’ll use $match to return only the book with the title \"Treasure of the Sun\"." ] }, { @@ -107,14 +108,14 @@ }, "outputs": [], "source": [ - "Bson yearFilter = gt(\"year\", 2010);\n", - "Bson matchStage = match(yearFilter);\n", - "\n", + "Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n", + "Bson matchStage = match(titleFilter);\n", "List aggregationPipeline = List.of(matchStage);\n", "\n", "AggregateIterable result = books.aggregate(aggregationPipeline);\n", "\n", "// Iterate through the results\n", + "\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" From 0fc4e7725bf8818aa6838770256514a947768328 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 15:52:01 -0300 Subject: [PATCH 04/15] Add $limit and $project aggregation example --- java/100_aggregation_pipeline_match.ipynb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index c57354c..2de8905 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -126,9 +126,9 @@ "id": "9f6caab3", "metadata": {}, "source": [ - "### $limit\n", + "### $limit and $project\n", "\n", - "We'll get books published after 2010 and limit the results to 10 documents." + "We’ll return books published after 2010, limit the result to 10 documents, and project only the year and title fields." ] }, { @@ -144,9 +144,15 @@ "source": [ "Bson yearFilter = gt(\"year\", 2010);\n", "Bson matchStage = match(yearFilter);\n", + "Bson projectStage = project(\n", + " fields(\n", + " include(\"year\", \"title\"),\n", + " excludeId()\n", + " ) \n", + ");\n", "Bson limitStage = limit(10);\n", "\n", - "List aggregationPipeline = List.of(matchStage, limitStage);\n", + "List aggregationPipeline = List.of(matchStage, projectStage, limitStage);\n", "\n", "AggregateIterable result = books.aggregate(aggregationPipeline);\n", "\n", From 19d5ff2845cdeb3e7726fb8cbfd1d2018ddf6992 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 16:04:57 -0300 Subject: [PATCH 05/15] Add dedicated $project aggregation example and explanation --- java/100_aggregation_pipeline_match.ipynb | 63 +++++++++++++++++++---- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 2de8905..40c0312 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -58,7 +58,7 @@ "import static com.mongodb.client.model.Aggregates.match;\n", "import static com.mongodb.client.model.Aggregates.limit;\n", "import static com.mongodb.client.model.Aggregates.sort;\n", - "import static com.mongodb.client.model.Sorts.ascending;\n", + "import static com.mongodb.client.model.Sorts.descending;\n", "\n", "import org.bson.Document;\n", "import org.bson.conversions.Bson;\n", @@ -121,12 +121,52 @@ "}" ] }, + { + "cell_type": "markdown", + "id": "74f5fd5a", + "metadata": {}, + "source": [ + "## $project\n", + "\n", + "We’ll project only the `title` and `year` fields for the book \"Treasure of the Sun\", excluding the `_id` field from the result." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "50b8aa38", + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [], + "source": [ + "Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n", + "Bson matchStage = match(titleFilter);\n", + "\n", + "Bson projectStage = project(\n", + " fields(\n", + " include(\"title\", \"year\"),\n", + " excludeId()\n", + " )\n", + ");\n", + "\n", + "List aggregationPipeline = List.of(matchStage, projectStage);\n", + "\n", + "AggregateIterable result = books.aggregate(aggregationPipeline);\n", + "\n", + "for (Document doc : result) {\n", + " System.out.println(\"book: \" + doc.toJson());\n", + "}\n" + ] + }, { "cell_type": "markdown", "id": "9f6caab3", "metadata": {}, "source": [ - "### $limit and $project\n", + "### $limit\n", "\n", "We’ll return books published after 2010, limit the result to 10 documents, and project only the year and title fields." ] @@ -174,7 +214,7 @@ "source": [ "### $sort\n", "\n", - "We'll sort the results by publication year in ascending order." + "Use `$sort` to order the books by the number of pages in descending order, returning the longest books first." ] }, { @@ -188,15 +228,20 @@ }, "outputs": [], "source": [ - "Bson yearFilter = gt(\"year\", 2010);\n", - "Bson matchStage = match(yearFilter);\n", + "Bson projectStage = project(\n", + " fields(\n", + " include(\"title\", \"pages\"),\n", + " excludeId()\n", + " )\n", + ");\n", + "\n", + "Bson sortStage = sort(descending(\"pages\"));\n", "Bson limitStage = limit(10);\n", - "Bson sortStage = sort(ascending(\"year\"));\n", "\n", "List aggregationPipeline = List.of(\n", - " matchStage,\n", - " limitStage,\n", - " sortStage\n", + " projectStage,\n", + " sortStage,\n", + " limitStage\n", ");\n", "\n", "AggregateIterable result = books.aggregate(aggregationPipeline);\n", From c44f1511bd83c6160f03ce3300756d2873e9fdef Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 16:13:21 -0300 Subject: [PATCH 06/15] refactor: simplify aggregation limit examples and improve descriptions --- java/100_aggregation_pipeline_match.ipynb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 40c0312..32cf7f3 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -168,7 +168,7 @@ "source": [ "### $limit\n", "\n", - "We’ll return books published after 2010, limit the result to 10 documents, and project only the year and title fields." + "We’ll first match books published after 2010, then use `$limit` to return only 3 documents." ] }, { @@ -184,15 +184,9 @@ "source": [ "Bson yearFilter = gt(\"year\", 2010);\n", "Bson matchStage = match(yearFilter);\n", - "Bson projectStage = project(\n", - " fields(\n", - " include(\"year\", \"title\"),\n", - " excludeId()\n", - " ) \n", - ");\n", - "Bson limitStage = limit(10);\n", + "Bson limitStage = limit(3);\n", "\n", - "List aggregationPipeline = List.of(matchStage, projectStage, limitStage);\n", + "List aggregationPipeline = List.of(matchStage, limitStage);\n", "\n", "AggregateIterable result = books.aggregate(aggregationPipeline);\n", "\n", From 38e22e9d68af15f216d50dc475df48831f704ba1 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 16:16:10 -0300 Subject: [PATCH 07/15] refactor: improve aggregation sort example and description --- java/100_aggregation_pipeline_match.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 32cf7f3..3773954 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -166,7 +166,7 @@ "id": "9f6caab3", "metadata": {}, "source": [ - "### $limit\n", + "## $limit\n", "\n", "We’ll first match books published after 2010, then use `$limit` to return only 3 documents." ] @@ -206,9 +206,9 @@ "id": "worth-windows", "metadata": {}, "source": [ - "### $sort\n", + "## $sort\n", "\n", - "Use `$sort` to order the books by the number of pages in descending order, returning the longest books first." + "We’ll 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." ] }, { @@ -230,7 +230,7 @@ ");\n", "\n", "Bson sortStage = sort(descending(\"pages\"));\n", - "Bson limitStage = limit(10);\n", + "Bson limitStage = limit(3);\n", "\n", "List aggregationPipeline = List.of(\n", " projectStage,\n", From 1f84f7d4faa759032f86a029a6e2f6da4a01d3f2 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 16:42:20 -0300 Subject: [PATCH 08/15] refactor: simplify challenge boilerplate so learners can focus on pipeline stages --- java/100_aggregation_pipeline_match.ipynb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 3773954..409fecf 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -274,7 +274,15 @@ }, "outputs": [], "source": [ - "// type your code here" + "// TYPE YOUR CODE HERE\n", + "\n", + "AggregateIterable result = books.aggregate(\n", + " List.of() \n", + ");\n", + "\n", + "for (Document doc : result) {\n", + " System.out.println(\"book: \" + doc.toJson());\n", + "}" ] }, { @@ -298,7 +306,18 @@ }, "outputs": [], "source": [ - "// type your code here" + "// TYPE YOUR CODE HERE\n", + "\n", + "AggregateIterable result = books.aggregate(\n", + " List.of(\n", + " ,\n", + " \n", + " )\n", + ");\n", + "\n", + "for (Document doc : result) {\n", + " System.out.println(\"book: \" + doc.toJson());\n", + "}" ] } ], From 59f0f5c269cc5ff32f4094231b8af9c9b52c4e34 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 17:10:07 -0300 Subject: [PATCH 09/15] Including simple comments --- java/100_aggregation_pipeline_match.ipynb | 4 +++- java/101_aggregation_pipeline_arrays.ipynb | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 409fecf..b3a3328 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -115,7 +115,6 @@ "AggregateIterable result = books.aggregate(aggregationPipeline);\n", "\n", "// Iterate through the results\n", - "\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" @@ -156,6 +155,7 @@ "\n", "AggregateIterable result = books.aggregate(aggregationPipeline);\n", "\n", + "// Iterate through the results\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}\n" @@ -190,6 +190,7 @@ "\n", "AggregateIterable result = books.aggregate(aggregationPipeline);\n", "\n", + "// Iterate through the results\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" @@ -240,6 +241,7 @@ "\n", "AggregateIterable result = books.aggregate(aggregationPipeline);\n", "\n", + "// Iterate through the results\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" diff --git a/java/101_aggregation_pipeline_arrays.ipynb b/java/101_aggregation_pipeline_arrays.ipynb index 3d43229..10ef4cd 100644 --- a/java/101_aggregation_pipeline_arrays.ipynb +++ b/java/101_aggregation_pipeline_arrays.ipynb @@ -99,12 +99,12 @@ "id": "handled-symbol", "metadata": {}, "source": [ - "### $match: $all\n", + "### $match with the `$all` operator\n", "\n", "[arrays-reference](\n", "https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/using-arrays/simple-match-array)\n", "\n", - "If you want to find books whose `genres` array contains both \"Family Life\" and \"Fiction\", in any order, use:" + "If you want to find books whose `genres` array contains both `\"Family Life\"` and `\"Fiction\"`, in any order, use:" ] }, { From 0f9508424e3f428408823368e2737b1b78456ec0 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 17:13:27 -0300 Subject: [PATCH 10/15] refactor: improve array aggregation examples and explanatory notes --- java/100_aggregation_pipeline_match.ipynb | 1 - java/101_aggregation_pipeline_arrays.ipynb | 40 +++++++++++++--------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index b3a3328..038be58 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -67,7 +67,6 @@ "import org.bson.conversions.Bson;\n", "\n", "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", - "\n", "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", "\n", diff --git a/java/101_aggregation_pipeline_arrays.ipynb b/java/101_aggregation_pipeline_arrays.ipynb index 10ef4cd..9322a26 100644 --- a/java/101_aggregation_pipeline_arrays.ipynb +++ b/java/101_aggregation_pipeline_arrays.ipynb @@ -56,6 +56,9 @@ "import static com.mongodb.client.model.Filters.in;\n", "import static com.mongodb.client.model.Sorts.descending;\n", "\n", + "import static com.mongodb.client.model.Projections.include;\n", + "import static com.mongodb.client.model.Projections.excludeId;\n", + "import static com.mongodb.client.model.Projections.fields;\n", "\n", "import static com.mongodb.client.model.Aggregates.match;\n", "import static com.mongodb.client.model.Aggregates.project;\n", @@ -66,7 +69,6 @@ "import org.bson.conversions.Bson;\n", "\n", "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", - "\n", "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", "\n", @@ -118,15 +120,18 @@ }, "outputs": [], "source": [ - "AggregateIterable result = books.aggregate(Arrays.asList(\n", - " Aggregates.match(Filters.all(\"genres\", \"Family Life\", \"Fiction\")),\n", - " Aggregates.project(new Document()\n", - " .append(\"title\", 1)\n", - " .append(\"genres\", 1)\n", + "AggregateIterable result = books.aggregate(\n", + " List.of(\n", + " match(all(\"genres\", \"Family Life\", \"Fiction\")),\n", + " project(\n", + " fields(\n", + " include(\"title\", \"genres\"),\n", + " excludeId()\n", + " )\n", + " )\n", " )\n", - "));\n", + ");\n", "\n", - "// Iterate through the results\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" @@ -137,7 +142,7 @@ "id": "9f6caab3", "metadata": {}, "source": [ - "### $match: $in\n", + "### $match with the `$in` operator\n", "\n", "Use `$in` to find books where the `genres` array contains at least one of the specified values." ] @@ -153,15 +158,18 @@ }, "outputs": [], "source": [ - "AggregateIterable result = books.aggregate(Arrays.asList(\n", - " Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n", - " Aggregates.project(new Document()\n", - " .append(\"title\", 1)\n", - " .append(\"genres\", 1)\n", + "// Note: `Document` can also be used here because it implements `Bson`.\n", + "\n", + "AggregateIterable result = books.aggregate(\n", + " List.of(\n", + " Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n", + " Aggregates.project(new Document()\n", + " .append(\"title\", 1)\n", + " .append(\"genres\", 1)\n", + " )\n", " )\n", - "));\n", + ");\n", "\n", - "// Iterate through the results\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" From 3e771dd8b8da5542b2677bc3f5d8a2a9eb95ee34 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 17:32:24 -0300 Subject: [PATCH 11/15] refactor: improve unwind example readability and projection style --- java/101_aggregation_pipeline_arrays.ipynb | 2 ++ java/102_aggregation_pipeline_unwind.ipynb | 25 ++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/java/101_aggregation_pipeline_arrays.ipynb b/java/101_aggregation_pipeline_arrays.ipynb index 9322a26..11ef0a1 100644 --- a/java/101_aggregation_pipeline_arrays.ipynb +++ b/java/101_aggregation_pipeline_arrays.ipynb @@ -132,6 +132,7 @@ " )\n", ");\n", "\n", + "// Iterate through the results\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" @@ -170,6 +171,7 @@ " )\n", ");\n", "\n", + "// Iterate through the results\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" diff --git a/java/102_aggregation_pipeline_unwind.ipynb b/java/102_aggregation_pipeline_unwind.ipynb index 2772459..ad8554c 100644 --- a/java/102_aggregation_pipeline_unwind.ipynb +++ b/java/102_aggregation_pipeline_unwind.ipynb @@ -49,8 +49,16 @@ "import com.mongodb.client.model.Filters;\n", "import org.bson.Document;\n", "\n", - "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", + "import static com.mongodb.client.model.Filters.eq;\n", + "import static com.mongodb.client.model.Projections.include;\n", + "import static com.mongodb.client.model.Projections.excludeId;\n", + "import static com.mongodb.client.model.Projections.fields;\n", + "\n", + "import static com.mongodb.client.model.Aggregates.match;\n", + "import static com.mongodb.client.model.Aggregates.project;\n", + "import static com.mongodb.client.model.Aggregates.unwind;\n", "\n", + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", "\n", @@ -83,7 +91,7 @@ "source": [ "## $unwind\n", "\n", - "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" + "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." ] }, { @@ -97,14 +105,13 @@ }, "outputs": [], "source": [ - "AggregateIterable result = books.aggregate(List.of(\n", - " Aggregates.match(Filters.eq(\"_id\", \"0004127382\")),\n", - " Aggregates.unwind(\"$attributes\"), \n", - " Aggregates.project(new Document()\n", - " .append(\"title\", 1)\n", - " .append(\"attributes\", 1)\n", + "AggregateIterable result = books.aggregate(\n", + " List.of(\n", + " match(eq(\"_id\", \"0004127382\")),\n", + " unwind(\"$attributes\"),\n", + " project(fields(include(\"title\", \"attributes\"), excludeId()))\n", " )\n", - "));\n", + ");\n", "\n", "// Iterate through the results\n", "for (Document doc : result) {\n", From 74f31442d93c22997b04c9a4e9d0ae04711665c9 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 17:46:35 -0300 Subject: [PATCH 12/15] Including books collection --- java/103_aggregation_pipeline_lookup.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/103_aggregation_pipeline_lookup.ipynb b/java/103_aggregation_pipeline_lookup.ipynb index b18aa21..a6afc7e 100644 --- a/java/103_aggregation_pipeline_lookup.ipynb +++ b/java/103_aggregation_pipeline_lookup.ipynb @@ -55,7 +55,6 @@ "import org.bson.Document;\n", "\n", "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", - "\n", "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", "\n", @@ -72,7 +71,8 @@ "}\n", "\n", "MongoDatabase library = mongoClient.getDatabase(\"library\");\n", - "MongoCollection authors = library.getCollection(\"authors\");" + "MongoCollection authors = library.getCollection(\"authors\");\n", + "MongoCollection books = library.getCollection(\"books\");" ] }, { From 8fa7e651b062a5f078740dbaf87fced5b73b73f1 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 17:58:00 -0300 Subject: [PATCH 13/15] refactor: improve lookup challenge scaffolding and notebook guidance --- java/103_aggregation_pipeline_lookup.ipynb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/java/103_aggregation_pipeline_lookup.ipynb b/java/103_aggregation_pipeline_lookup.ipynb index a6afc7e..827c7de 100644 --- a/java/103_aggregation_pipeline_lookup.ipynb +++ b/java/103_aggregation_pipeline_lookup.ipynb @@ -128,7 +128,10 @@ "metadata": {}, "source": [ "### 1. Fetch books with their associated reviews\n", - "[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/lookup#-1-fetch-books-with-their-associated-reviews)" + "\n", + "Bonus: try adding a `$limit` stage as an extra improvement to reduce the output size.\n", + "\n", + "[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/lookup#-1-fetch-books-with-their-associated-reviews)\n" ] }, { @@ -142,7 +145,18 @@ }, "outputs": [], "source": [ - "// type your code here" + "// TYPE YOUR CODE HERE\n", + "\n", + "AggregateIterable result = books.aggregate(\n", + " List.of(\n", + " \n", + " )\n", + ");\n", + "\n", + "// Iterate through the results\n", + "for (Document doc : result) {\n", + " System.out.println(\"book: \" + doc.toJson());\n", + "}" ] } ], From d5c972acd0b71adde3cbe995d5a76879895aab55 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 18:03:47 -0300 Subject: [PATCH 14/15] refactor: improve group example wording and formatting --- java/104_aggregation_pipeline_group_by.ipynb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/java/104_aggregation_pipeline_group_by.ipynb b/java/104_aggregation_pipeline_group_by.ipynb index 586f590..8ba811e 100644 --- a/java/104_aggregation_pipeline_group_by.ipynb +++ b/java/104_aggregation_pipeline_group_by.ipynb @@ -46,6 +46,7 @@ "import com.mongodb.client.model.Filters;\n", "\n", "import static com.mongodb.client.model.Sorts.descending;\n", + "import static com.mongodb.client.model.Aggregates.limit;\n", "\n", "import org.bson.Document;\n", "import java.util.Arrays;\n", @@ -78,7 +79,7 @@ "source": [ "## $group\n", "\n", - "`$group` groups books by `year` and calculates the total number of pages for each group." + "Use `$group` to group books by `year` and calculate the total number of pages for each year and limit in 5 results." ] }, { @@ -89,8 +90,12 @@ "outputs": [], "source": [ "AggregateIterable result = books.aggregate(\n", - " Arrays.asList(\n", - " Aggregates.group(\"$year\", Accumulators.sum(\"totalPages\", \"$pages\"))\n", + " List.of(\n", + " Aggregates.group(\n", + " \"$year\",\n", + " Accumulators.sum(\"totalPages\", \"$pages\")\n", + " ),\n", + " limit(5)\n", " )\n", ");\n", "\n", From b5be334a4055f8c5e55644b1d5b9704b6d821cd3 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 8 May 2026 18:27:01 -0300 Subject: [PATCH 15/15] refactor: improve group challenges and add clearer aggregation hints --- java/104_aggregation_pipeline_group_by.ipynb | 57 +++++++++++++++++--- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/java/104_aggregation_pipeline_group_by.ipynb b/java/104_aggregation_pipeline_group_by.ipynb index 8ba811e..353009b 100644 --- a/java/104_aggregation_pipeline_group_by.ipynb +++ b/java/104_aggregation_pipeline_group_by.ipynb @@ -47,6 +47,8 @@ "\n", "import static com.mongodb.client.model.Sorts.descending;\n", "import static com.mongodb.client.model.Aggregates.limit;\n", + "import static com.mongodb.client.model.Aggregates.group;\n", + "import static com.mongodb.client.model.Aggregates.sort;\n", "\n", "import org.bson.Document;\n", "import java.util.Arrays;\n", @@ -69,7 +71,8 @@ "}\n", "\n", "MongoDatabase library = mongoClient.getDatabase(\"library\");\n", - "MongoCollection books = library.getCollection(\"books\");" + "MongoCollection books = library.getCollection(\"books\");\n", + "MongoCollection reviews = library.getCollection(\"reviews\");\n" ] }, { @@ -118,7 +121,10 @@ "id": "d96043ab", "metadata": {}, "source": [ - "### 1. Find the average book rating of all books\n", + "### 1. Find the average rating for each book\n", + "\n", + "Hint: use the `reviews` collection, group by `bookId`, and calculate the average of the `rating` field.\n", + "\n", "[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/group#-1-find-the-average-book-rating-of-all-books)" ] }, @@ -129,7 +135,17 @@ "metadata": {}, "outputs": [], "source": [ - "// type your code here" + "// TYPE YOUR CODE HERE\n", + "\n", + "AggregateIterable result = reviews.aggregate(\n", + " List.of(\n", + " \n", + " )\n", + ");\n", + "\n", + "for (Document doc : result) {\n", + " System.out.println(\"result: \" + doc.toJson());\n", + "}" ] }, { @@ -137,7 +153,9 @@ "id": "2b9bb53e", "metadata": {}, "source": [ - "### 2. Find users with the most number of reviews (Hint: use sum + sort)\n", + "### 2. Find the top 5 users with the highest number of reviews - use (Sort + Sum)\n", + "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", + "\n", "[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)" ] }, @@ -148,7 +166,16 @@ "metadata": {}, "outputs": [], "source": [ - "// type your code here" + "AggregateIterable result = reviews.aggregate(\n", + " List.of(\n", + " \n", + " \n", + " )\n", + ");\n", + "\n", + "for (Document doc : result) {\n", + " System.out.println(\"user: \" + doc.toJson());\n", + "}" ] }, { @@ -156,8 +183,11 @@ "id": "74e33d58", "metadata": {}, "source": [ - "### 3. Find users with the most number of reviews (Hint: use $sortByCount)\n", - "[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)" + "### 3. Find the top 5 users with the highest number of reviews - use (SortByCount)\n", + "\n", + "Hint: use `sortByCount` with the `name` field, and remember to use `$limit` to keep the output easier to inspect.\n", + "\n", + "[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" ] }, { @@ -167,7 +197,18 @@ "metadata": {}, "outputs": [], "source": [ - "// type your code here" + "// TYPE YOUR CODE HERE\n", + "\n", + "AggregateIterable result = reviews.aggregate(\n", + " List.of(\n", + " ,\n", + " \n", + " )\n", + ");\n", + "\n", + "for (Document doc : result) {\n", + " System.out.println(\"user: \" + doc.toJson());\n", + "}" ] } ],