0
0
MongoDBquery~10 mins

Array expressions ($size, $arrayElemAt, $filter) in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the size of the array field 'scores'.

MongoDB
{ $size: "$ [1] " }
Drag options to blanks, or click blank then click option'
Avalues
Bscore
Cscores
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong field name like 'score' instead of 'scores'.
Forgetting to use quotes around the field name.
2fill in blank
medium

Complete the code to get the first element of the 'grades' array using $arrayElemAt.

MongoDB
{ $arrayElemAt: [ "$grades", [1] ] }
Drag options to blanks, or click blank then click option'
A1
B0
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the index which returns the second element.
Using negative indexes which are not supported here.
3fill in blank
hard

Fix the error in the $filter expression to select elements greater than 10 from 'values'.

MongoDB
{ $filter: { input: "$values", as: "item", cond: { $gt: [ "$$ [1] ", 10 ] } } }
Drag options to blanks, or click blank then click option'
Avalue
Belement
Cval
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than declared in 'as'.
Forgetting the double dollar signs ($$) before the variable.
4fill in blank
hard

Fill both blanks to filter 'scores' array for values less than 50 and get its size.

MongoDB
{ $size: { $filter: { input: "$scores", as: "score", cond: { $lt: [ "$$ [1] ", [2] ] } } } }
Drag options to blanks, or click blank then click option'
Ascore
Bvalue
C50
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name in the condition.
Using a wrong number for comparison.
5fill in blank
hard

Fill all three blanks to get the second element of filtered 'items' array where elements are greater than 5.

MongoDB
{ $arrayElemAt: [ { $filter: { input: "$items", as: "elem", cond: { $gt: [ "$$ [1] ", [2] ] } } }, [3] ] }
Drag options to blanks, or click blank then click option'
Aelem
B5
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name in condition.
Using wrong comparison number.
Using wrong index for the second element.