|
222 | 222 | " });" |
223 | 223 | ] |
224 | 224 | }, |
| 225 | + { |
| 226 | + "cell_type": "markdown", |
| 227 | + "id": "fc634597", |
| 228 | + "metadata": {}, |
| 229 | + "source": [ |
| 230 | + "# Limit and Count documents\n", |
| 231 | + "\n", |
| 232 | + "## Show only 5 books" |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "code", |
| 237 | + "execution_count": null, |
| 238 | + "id": "6aae4ffc", |
| 239 | + "metadata": {}, |
| 240 | + "outputs": [], |
| 241 | + "source": [ |
| 242 | + "const showOnly5Books = { $limit: 5 };\n", |
| 243 | + "const projection = { $project: \n", |
| 244 | + " {\n", |
| 245 | + " _id: 0,\n", |
| 246 | + " title: 1,\n", |
| 247 | + " theAuthors: \"$authors.name\", // we're adding this field to show the authors' names using a expression\n", |
| 248 | + " genres: 1\n", |
| 249 | + " }\n", |
| 250 | + "};\n", |
| 251 | + "\n", |
| 252 | + "const pipeline = [showOnly5Books, projection];\n", |
| 253 | + "\n", |
| 254 | + "const cursor = await books.aggregate(pipeline);\n", |
| 255 | + "\n", |
| 256 | + "await cursor.forEach((b) => {\n", |
| 257 | + " console.log(b);\n", |
| 258 | + "});\n" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "markdown", |
| 263 | + "id": "1546e8b9", |
| 264 | + "metadata": {}, |
| 265 | + "source": [ |
| 266 | + "## Count all books" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": null, |
| 272 | + "id": "3212e91f", |
| 273 | + "metadata": {}, |
| 274 | + "outputs": [], |
| 275 | + "source": [ |
| 276 | + "const countBooks = { $count: \"totalBooks\" };\n", |
| 277 | + "\n", |
| 278 | + "const pipeline = [countBooks];\n", |
| 279 | + "\n", |
| 280 | + "const cursor = await books.aggregate(pipeline);\n", |
| 281 | + "\n", |
| 282 | + "await cursor.forEach((b) => {\n", |
| 283 | + " console.log(b);\n", |
| 284 | + "});" |
| 285 | + ] |
| 286 | + }, |
| 287 | + { |
| 288 | + "cell_type": "markdown", |
| 289 | + "id": "1ccf064b", |
| 290 | + "metadata": {}, |
| 291 | + "source": [ |
| 292 | + "## Sorting" |
| 293 | + ] |
| 294 | + }, |
| 295 | + { |
| 296 | + "cell_type": "code", |
| 297 | + "execution_count": null, |
| 298 | + "id": "f080a8ef", |
| 299 | + "metadata": {}, |
| 300 | + "outputs": [], |
| 301 | + "source": [ |
| 302 | + "const showOnly5Books = { $limit: 5 };\n", |
| 303 | + "const projection = { $project: \n", |
| 304 | + " {\n", |
| 305 | + " _id: 0,\n", |
| 306 | + " title: 1,\n", |
| 307 | + " theAuthors: \"$authors.name\", // we're adding this field to show the authors' names using a expression\n", |
| 308 | + " genres: 1\n", |
| 309 | + " }\n", |
| 310 | + "};\n", |
| 311 | + "const sortByName = { $sort: { title: 1 } };\n", |
| 312 | + "\n", |
| 313 | + "const pipeline = [showOnly5Books, projection, sortByName];\n", |
| 314 | + "\n", |
| 315 | + "const cursor = await books.aggregate(pipeline);\n", |
| 316 | + "\n", |
| 317 | + "await cursor.forEach((b) => {\n", |
| 318 | + " console.log(b);\n", |
| 319 | + "});\n" |
| 320 | + ] |
| 321 | + }, |
225 | 322 | { |
226 | 323 | "cell_type": "markdown", |
227 | 324 | "id": "f5ca6684", |
|
237 | 334 | "source": [ |
238 | 335 | "### Find books with more than 2 available copies.\n", |
239 | 336 | "\n", |
240 | | - "[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/match-project#-1-find-books-with-more-than-2-available-copies)" |
| 337 | + "[Solution here](https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/simple-queries/project#-1-find-books-with-more-than-2-available-copies)" |
241 | 338 | ] |
242 | 339 | }, |
243 | 340 | { |
|
257 | 354 | "source": [ |
258 | 355 | "### Find books with more than 2 available copies. Return only book titles and publication year.\n", |
259 | 356 | "\n", |
260 | | - "[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/aggregation/match-project#-2-find-books-with-more-than-2-available-copies-return-only-book-titles-and-publication-year)" |
| 357 | + "[Solution here](https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/simple-queries/project#-2-find-books-with-more-than-2-available-copies-return-only-book-titles-and-publication-year)" |
261 | 358 | ] |
262 | 359 | }, |
263 | 360 | { |
|
0 commit comments