Skip to content

Commit 3e771dd

Browse files
refactor: improve unwind example readability and projection style
1 parent 0f95084 commit 3e771dd

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

java/101_aggregation_pipeline_arrays.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
" )\n",
133133
");\n",
134134
"\n",
135+
"// Iterate through the results\n",
135136
"for (Document doc : result) {\n",
136137
" System.out.println(\"book: \" + doc.toJson());\n",
137138
"}"
@@ -170,6 +171,7 @@
170171
" )\n",
171172
");\n",
172173
"\n",
174+
"// Iterate through the results\n",
173175
"for (Document doc : result) {\n",
174176
" System.out.println(\"book: \" + doc.toJson());\n",
175177
"}"

java/102_aggregation_pipeline_unwind.ipynb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@
4949
"import com.mongodb.client.model.Filters;\n",
5050
"import org.bson.Document;\n",
5151
"\n",
52-
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\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",
5360
"\n",
61+
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
5462
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
5563
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
5664
"\n",
@@ -83,7 +91,7 @@
8391
"source": [
8492
"## $unwind\n",
8593
"\n",
86-
"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."
8795
]
8896
},
8997
{
@@ -97,14 +105,13 @@
97105
},
98106
"outputs": [],
99107
"source": [
100-
"AggregateIterable<Document> result = books.aggregate(List.of(\n",
101-
" Aggregates.match(Filters.eq(\"_id\", \"0004127382\")),\n",
102-
" Aggregates.unwind(\"$attributes\"), \n",
103-
" Aggregates.project(new Document()\n",
104-
" .append(\"title\", 1)\n",
105-
" .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",
106113
" )\n",
107-
"));\n",
114+
");\n",
108115
"\n",
109116
"// Iterate through the results\n",
110117
"for (Document doc : result) {\n",

0 commit comments

Comments
 (0)