|
58 | 58 | "import static com.mongodb.client.model.Aggregates.match;\n", |
59 | 59 | "import static com.mongodb.client.model.Aggregates.limit;\n", |
60 | 60 | "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", |
62 | 62 | "\n", |
63 | 63 | "import org.bson.Document;\n", |
64 | 64 | "import org.bson.conversions.Bson;\n", |
|
121 | 121 | "}" |
122 | 122 | ] |
123 | 123 | }, |
| 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 | + }, |
124 | 164 | { |
125 | 165 | "cell_type": "markdown", |
126 | 166 | "id": "9f6caab3", |
127 | 167 | "metadata": {}, |
128 | 168 | "source": [ |
129 | | - "### $limit and $project\n", |
| 169 | + "### $limit\n", |
130 | 170 | "\n", |
131 | 171 | "We’ll return books published after 2010, limit the result to 10 documents, and project only the year and title fields." |
132 | 172 | ] |
|
174 | 214 | "source": [ |
175 | 215 | "### $sort\n", |
176 | 216 | "\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." |
178 | 218 | ] |
179 | 219 | }, |
180 | 220 | { |
|
188 | 228 | }, |
189 | 229 | "outputs": [], |
190 | 230 | "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", |
193 | 239 | "Bson limitStage = limit(10);\n", |
194 | | - "Bson sortStage = sort(ascending(\"year\"));\n", |
195 | 240 | "\n", |
196 | 241 | "List<Bson> aggregationPipeline = List.of(\n", |
197 | | - " matchStage,\n", |
198 | | - " limitStage,\n", |
199 | | - " sortStage\n", |
| 242 | + " projectStage,\n", |
| 243 | + " sortStage,\n", |
| 244 | + " limitStage\n", |
200 | 245 | ");\n", |
201 | 246 | "\n", |
202 | 247 | "AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n", |
|
0 commit comments