Skip to content

Commit 19d5ff2

Browse files
Add dedicated $project aggregation example and explanation
1 parent 0fc4e77 commit 19d5ff2

1 file changed

Lines changed: 54 additions & 9 deletions

File tree

‎java/100_aggregation_pipeline_match.ipynb‎

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"import static com.mongodb.client.model.Aggregates.match;\n",
5959
"import static com.mongodb.client.model.Aggregates.limit;\n",
6060
"import static com.mongodb.client.model.Aggregates.sort;\n",
61-
"import static com.mongodb.client.model.Sorts.ascending;\n",
61+
"import static com.mongodb.client.model.Sorts.descending;\n",
6262
"\n",
6363
"import org.bson.Document;\n",
6464
"import org.bson.conversions.Bson;\n",
@@ -121,12 +121,52 @@
121121
"}"
122122
]
123123
},
124+
{
125+
"cell_type": "markdown",
126+
"id": "74f5fd5a",
127+
"metadata": {},
128+
"source": [
129+
"## $project\n",
130+
"\n",
131+
"We’ll project only the `title` and `year` fields for the book \"Treasure of the Sun\", excluding the `_id` field from the result."
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": null,
137+
"id": "50b8aa38",
138+
"metadata": {
139+
"vscode": {
140+
"languageId": "java"
141+
}
142+
},
143+
"outputs": [],
144+
"source": [
145+
"Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n",
146+
"Bson matchStage = match(titleFilter);\n",
147+
"\n",
148+
"Bson projectStage = project(\n",
149+
" fields(\n",
150+
" include(\"title\", \"year\"),\n",
151+
" excludeId()\n",
152+
" )\n",
153+
");\n",
154+
"\n",
155+
"List<Bson> aggregationPipeline = List.of(matchStage, projectStage);\n",
156+
"\n",
157+
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
158+
"\n",
159+
"for (Document doc : result) {\n",
160+
" System.out.println(\"book: \" + doc.toJson());\n",
161+
"}\n"
162+
]
163+
},
124164
{
125165
"cell_type": "markdown",
126166
"id": "9f6caab3",
127167
"metadata": {},
128168
"source": [
129-
"### $limit and $project\n",
169+
"### $limit\n",
130170
"\n",
131171
"We’ll return books published after 2010, limit the result to 10 documents, and project only the year and title fields."
132172
]
@@ -174,7 +214,7 @@
174214
"source": [
175215
"### $sort\n",
176216
"\n",
177-
"We'll sort the results by publication year in ascending order."
217+
"Use `$sort` to order the books by the number of pages in descending order, returning the longest books first."
178218
]
179219
},
180220
{
@@ -188,15 +228,20 @@
188228
},
189229
"outputs": [],
190230
"source": [
191-
"Bson yearFilter = gt(\"year\", 2010);\n",
192-
"Bson matchStage = match(yearFilter);\n",
231+
"Bson projectStage = project(\n",
232+
" fields(\n",
233+
" include(\"title\", \"pages\"),\n",
234+
" excludeId()\n",
235+
" )\n",
236+
");\n",
237+
"\n",
238+
"Bson sortStage = sort(descending(\"pages\"));\n",
193239
"Bson limitStage = limit(10);\n",
194-
"Bson sortStage = sort(ascending(\"year\"));\n",
195240
"\n",
196241
"List<Bson> aggregationPipeline = List.of(\n",
197-
" matchStage,\n",
198-
" limitStage,\n",
199-
" sortStage\n",
242+
" projectStage,\n",
243+
" sortStage,\n",
244+
" limitStage\n",
200245
");\n",
201246
"\n",
202247
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",

0 commit comments

Comments
 (0)