Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: improve array aggregation examples and explanatory notes
  • Loading branch information
ricardohsmello committed May 8, 2026
commit 0f9508424e3f428408823368e2737b1b78456ec0
1 change: 0 additions & 1 deletion java/100_aggregation_pipeline_match.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 24 additions & 16 deletions java/101_aggregation_pipeline_arrays.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -118,15 +120,18 @@
},
"outputs": [],
"source": [
"AggregateIterable<Document> 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<Document> 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",
"}"
Expand All @@ -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."
]
Expand All @@ -153,15 +158,18 @@
},
"outputs": [],
"source": [
"AggregateIterable<Document> 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<Document> 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",
"}"
Expand Down