Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 99 additions & 2 deletions javascript/100_aggregation_pipeline_match.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,103 @@
" });"
]
},
{
"cell_type": "markdown",
"id": "fc634597",
"metadata": {},
"source": [
"# Limit and Count documents\n",
"\n",
"## Show only 5 books"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6aae4ffc",
"metadata": {},
"outputs": [],
"source": [
"const showOnly5Books = { $limit: 5 };\n",
"const projection = { $project: \n",
" {\n",
" _id: 0,\n",
" title: 1,\n",
" theAuthors: \"$authors.name\", // we're adding this field to show the authors' names using a expression\n",
" genres: 1\n",
" }\n",
"};\n",
"\n",
"const pipeline = [showOnly5Books, projection];\n",
"\n",
"const cursor = await books.aggregate(pipeline);\n",
"\n",
"await cursor.forEach((b) => {\n",
" console.log(b);\n",
"});\n"
]
},
{
"cell_type": "markdown",
"id": "1546e8b9",
"metadata": {},
"source": [
"## Count all books"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3212e91f",
"metadata": {},
"outputs": [],
"source": [
"const countBooks = { $count: \"totalBooks\" };\n",
"\n",
"const pipeline = [countBooks];\n",
"\n",
"const cursor = await books.aggregate(pipeline);\n",
"\n",
"await cursor.forEach((b) => {\n",
" console.log(b);\n",
"});"
]
},
{
"cell_type": "markdown",
"id": "1ccf064b",
"metadata": {},
"source": [
"## Sorting"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f080a8ef",
"metadata": {},
"outputs": [],
"source": [
"const showOnly5Books = { $limit: 5 };\n",
"const projection = { $project: \n",
" {\n",
" _id: 0,\n",
" title: 1,\n",
" theAuthors: \"$authors.name\", // we're adding this field to show the authors' names using a expression\n",
" genres: 1\n",
" }\n",
"};\n",
"const sortByName = { $sort: { title: 1 } };\n",
"\n",
"const pipeline = [showOnly5Books, projection, sortByName];\n",
"\n",
"const cursor = await books.aggregate(pipeline);\n",
"\n",
"await cursor.forEach((b) => {\n",
" console.log(b);\n",
"});\n"
]
},
{
"cell_type": "markdown",
"id": "f5ca6684",
Expand All @@ -237,7 +334,7 @@
"source": [
"### Find books with more than 2 available copies.\n",
"\n",
"[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)"
"[Solution here](https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/simple-queries/project#-1-find-books-with-more-than-2-available-copies)"
]
},
{
Expand All @@ -257,7 +354,7 @@
"source": [
"### Find books with more than 2 available copies. Return only book titles and publication year.\n",
"\n",
"[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)"
"[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)"
]
},
{
Expand Down
60 changes: 60 additions & 0 deletions javascript/101_aggregation_pipeline_arrays.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,66 @@
" console.log(`book: ${JSON.stringify(doc)}`);\n",
"});"
]
},
{
"cell_type": "markdown",
"id": "b1b980a9",
"metadata": {},
"source": [
"## $elemMatch"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fca82b33",
"metadata": {},
"outputs": [],
"source": [
"const result = await books.aggregate([\n",
" { $match: { attributes: { \n",
" $elemMatch: { key: \"edition\", value: \"1st\" } } } \n",
" },\n",
"]).toArray();\n",
"\n",
"result"
]
},
{
"cell_type": "markdown",
"id": "7dc0bb23",
"metadata": {},
"source": [
"## Get only the edition attribute"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c7b2b77",
"metadata": {},
"outputs": [],
"source": [
"const result = await books.aggregate([\n",
" { $match: { attributes: { \n",
" $elemMatch: { key: \"edition\", value: \"1st\" } } } \n",
" },\n",
" { $unwind : \"$attributes\" },\n",
" { $match: \n",
" {\"attributes.key\": \"msrp\", \n",
" \"attributes.value\": 9.99} \n",
" },\n",
" {\n",
" $project: {\n",
" title: 1,\n",
" attributes: 1\n",
" }\n",
" } \n",
" \n",
"]).toArray();\n",
"\n",
"result"
]
}
],
"metadata": {
Expand Down
Loading