Skip to content

Commit 0f95084

Browse files
refactor: improve array aggregation examples and explanatory notes
1 parent 59f0f5c commit 0f95084

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

java/100_aggregation_pipeline_match.ipynb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
"import org.bson.conversions.Bson;\n",
6868
"\n",
6969
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
70-
"\n",
7170
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
7271
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
7372
"\n",

java/101_aggregation_pipeline_arrays.ipynb

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
"import static com.mongodb.client.model.Filters.in;\n",
5757
"import static com.mongodb.client.model.Sorts.descending;\n",
5858
"\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",
5962
"\n",
6063
"import static com.mongodb.client.model.Aggregates.match;\n",
6164
"import static com.mongodb.client.model.Aggregates.project;\n",
@@ -66,7 +69,6 @@
6669
"import org.bson.conversions.Bson;\n",
6770
"\n",
6871
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
69-
"\n",
7072
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
7173
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
7274
"\n",
@@ -118,15 +120,18 @@
118120
},
119121
"outputs": [],
120122
"source": [
121-
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
122-
" Aggregates.match(Filters.all(\"genres\", \"Family Life\", \"Fiction\")),\n",
123-
" Aggregates.project(new Document()\n",
124-
" .append(\"title\", 1)\n",
125-
" .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",
126132
" )\n",
127-
"));\n",
133+
");\n",
128134
"\n",
129-
"// Iterate through the results\n",
130135
"for (Document doc : result) {\n",
131136
" System.out.println(\"book: \" + doc.toJson());\n",
132137
"}"
@@ -137,7 +142,7 @@
137142
"id": "9f6caab3",
138143
"metadata": {},
139144
"source": [
140-
"### $match: $in\n",
145+
"### $match with the `$in` operator\n",
141146
"\n",
142147
"Use `$in` to find books where the `genres` array contains at least one of the specified values."
143148
]
@@ -153,15 +158,18 @@
153158
},
154159
"outputs": [],
155160
"source": [
156-
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
157-
" Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n",
158-
" Aggregates.project(new Document()\n",
159-
" .append(\"title\", 1)\n",
160-
" .append(\"genres\", 1)\n",
161+
"// Note: `Document` can also be used here because it implements `Bson`.\n",
162+
"\n",
163+
"AggregateIterable<Document> result = books.aggregate(\n",
164+
" List.of(\n",
165+
" Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n",
166+
" Aggregates.project(new Document()\n",
167+
" .append(\"title\", 1)\n",
168+
" .append(\"genres\", 1)\n",
169+
" )\n",
161170
" )\n",
162-
"));\n",
171+
");\n",
163172
"\n",
164-
"// Iterate through the results\n",
165173
"for (Document doc : result) {\n",
166174
" System.out.println(\"book: \" + doc.toJson());\n",
167175
"}"

0 commit comments

Comments
 (0)