diff --git a/java/100_aggregation_pipeline_match.ipynb b/java/100_aggregation_pipeline_match.ipynb index 1090300..038be58 100644 --- a/java/100_aggregation_pipeline_match.ipynb +++ b/java/100_aggregation_pipeline_match.ipynb @@ -37,22 +37,28 @@ "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", "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.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 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", - "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", @@ -60,6 +66,10 @@ "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", + "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", @@ -83,7 +93,7 @@ "source": [ "## $match\n", "\n", - "We'll get all books written after 2010." + "We’ll use $match to return only the book with the title \"Treasure of the Sun\"." ] }, { @@ -97,9 +107,8 @@ }, "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", @@ -110,14 +119,55 @@ "}" ] }, + { + "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", + "// Iterate through the results\n", + "for (Document doc : result) {\n", + " System.out.println(\"book: \" + doc.toJson());\n", + "}\n" + ] + }, { "cell_type": "markdown", "id": "9f6caab3", "metadata": {}, "source": [ - "### $limit\n", + "## $limit\n", "\n", - "We'll get books published after 2010 and limit the results to 10 documents." + "We’ll first match books published after 2010, then use `$limit` to return only 3 documents." ] }, { @@ -133,12 +183,13 @@ "source": [ "Bson yearFilter = gt(\"year\", 2010);\n", "Bson matchStage = match(yearFilter);\n", - "Bson limitStage = limit(10);\n", + "Bson limitStage = limit(3);\n", "\n", "List aggregationPipeline = List.of(matchStage, limitStage);\n", "\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", "}" @@ -155,9 +206,9 @@ "id": "worth-windows", "metadata": {}, "source": [ - "### $sort\n", + "## $sort\n", "\n", - "We'll sort the results by publication year in ascending order." + "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." ] }, { @@ -171,19 +222,25 @@ }, "outputs": [], "source": [ - "Bson yearFilter = gt(\"year\", 2010);\n", - "Bson matchStage = match(yearFilter);\n", - "Bson limitStage = limit(10);\n", - "Bson sortStage = sort(ascending(\"year\"));\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(3);\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", "\n", + "// Iterate through the results\n", "for (Document doc : result) {\n", " System.out.println(\"book: \" + doc.toJson());\n", "}" @@ -218,7 +275,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", + "}" ] }, { @@ -242,7 +307,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", + "}" ] } ], diff --git a/java/101_aggregation_pipeline_arrays.ipynb b/java/101_aggregation_pipeline_arrays.ipynb index e6f863e..11ef0a1 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", @@ -55,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", @@ -64,6 +68,10 @@ "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", + "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", @@ -93,12 +101,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:" ] }, { @@ -112,13 +120,17 @@ }, "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", @@ -131,7 +143,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." ] @@ -147,13 +159,17 @@ }, "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", diff --git a/java/102_aggregation_pipeline_unwind.ipynb b/java/102_aggregation_pipeline_unwind.ipynb index 8f1d901..ad8554c 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,19 @@ "import com.mongodb.client.model.Filters;\n", "import org.bson.Document;\n", "\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", "// Set your connection String\n", "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", "\n", @@ -77,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." ] }, { @@ -91,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", diff --git a/java/103_aggregation_pipeline_lookup.ipynb b/java/103_aggregation_pipeline_lookup.ipynb index d90aeca..827c7de 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,10 @@ "\n", "import org.bson.Document;\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", "// Set your connection String\n", "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", "\n", @@ -66,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\");" ] }, { @@ -122,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" ] }, { @@ -136,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", + "}" ] } ], diff --git a/java/104_aggregation_pipeline_group_by.ipynb b/java/104_aggregation_pipeline_group_by.ipynb index 6baabb1..353009b 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", @@ -45,10 +46,18 @@ "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", + "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", "\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", @@ -62,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" ] }, { @@ -72,7 +82,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." ] }, { @@ -83,8 +93,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", @@ -107,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)" ] }, @@ -118,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", + "}" ] }, { @@ -126,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)" ] }, @@ -137,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", + "}" ] }, { @@ -145,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" ] }, { @@ -156,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", + "}" ] } ], 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",