Skip to content

Commit 7bf9817

Browse files
committed
Added limit count sort and array examples
1 parent 473cd91 commit 7bf9817

2 files changed

Lines changed: 159 additions & 2 deletions

File tree

‎javascript/100_aggregation_pipeline_match.ipynb‎

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,103 @@
222222
" });"
223223
]
224224
},
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+
},
225322
{
226323
"cell_type": "markdown",
227324
"id": "f5ca6684",
@@ -237,7 +334,7 @@
237334
"source": [
238335
"### Find books with more than 2 available copies.\n",
239336
"\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)"
241338
]
242339
},
243340
{
@@ -257,7 +354,7 @@
257354
"source": [
258355
"### Find books with more than 2 available copies. Return only book titles and publication year.\n",
259356
"\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)"
261358
]
262359
},
263360
{

‎javascript/101_aggregation_pipeline_arrays.ipynb‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,66 @@
139139
" console.log(`book: ${JSON.stringify(doc)}`);\n",
140140
"});"
141141
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"id": "b1b980a9",
146+
"metadata": {},
147+
"source": [
148+
"## $elemMatch"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"id": "fca82b33",
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"const result = await books.aggregate([\n",
159+
" { $match: { attributes: { \n",
160+
" $elemMatch: { key: \"edition\", value: \"1st\" } } } \n",
161+
" },\n",
162+
"]).toArray();\n",
163+
"\n",
164+
"result"
165+
]
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"id": "7dc0bb23",
170+
"metadata": {},
171+
"source": [
172+
"## Get only the edition attribute"
173+
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": null,
178+
"id": "2c7b2b77",
179+
"metadata": {},
180+
"outputs": [],
181+
"source": [
182+
"const result = await books.aggregate([\n",
183+
" { $match: { attributes: { \n",
184+
" $elemMatch: { key: \"edition\", value: \"1st\" } } } \n",
185+
" },\n",
186+
" { $unwind : \"$attributes\" },\n",
187+
" { $match: \n",
188+
" {\"attributes.key\": \"msrp\", \n",
189+
" \"attributes.value\": 9.99} \n",
190+
" },\n",
191+
" {\n",
192+
" $project: {\n",
193+
" title: 1,\n",
194+
" attributes: 1\n",
195+
" }\n",
196+
" } \n",
197+
" \n",
198+
"]).toArray();\n",
199+
"\n",
200+
"result"
201+
]
142202
}
143203
],
144204
"metadata": {

0 commit comments

Comments
 (0)