Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find documents where the array 'tags' has exactly 3 elements.
MongoDB
{ tags: { $[1]: 3 } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' or 'count' instead of '$size' causes errors.
Omitting the dollar sign before 'size'.
✗ Incorrect
The $size operator matches arrays with the specified number of elements.
2fill in blank
mediumComplete the aggregation pipeline stage to add a field 'tagCount' with the length of the 'tags' array.
MongoDB
{ $addFields: { tagCount: { $[1]: "$tags" } } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using operators like $length or $count which do not exist in MongoDB aggregation.
Forgetting to use quotes around the field name.
✗ Incorrect
The $size operator returns the number of elements in an array field.
3fill in blank
hardFix the error in the query to find documents where 'items' array length is 5.
MongoDB
{ items: { $[1]: 5 } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $length or $count which are invalid operators.
Missing the dollar sign before 'size'.
✗ Incorrect
The correct operator to check array length in MongoDB queries is $size.
4fill in blank
hardFill both blanks to create a match stage that filters documents where 'comments' array length is greater than 2.
MongoDB
{ $match: { comments: { $[1]: { $[2]: 2 } } } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt instead of $gt for greater than comparison.
Using $eq which checks equality, not greater than.
✗ Incorrect
Use $size to check array length and $gt to compare if length is greater than 2.
5fill in blank
hardFill both blanks to create an aggregation stage that filters documents with 'reviews' array length equal to 4 and adds a field 'reviewCount' with that length.
MongoDB
{ $match: { reviews: { $[1]: 4 } } }, { $addFields: { reviewCount: { $[2]: "$reviews" } } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' which is not a valid operator.
Using '$size' as a string in $match which expects operator without quotes.
✗ Incorrect
Use $size in both $match and $addFields stages to check and assign array length.