0
0
MongoDBquery~10 mins

$size operator for array length 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 find documents where the array 'tags' has exactly 3 elements.

MongoDB
{ tags: { $[1]: 3 } }
Drag options to blanks, or click blank then click option'
AlengthOf
Blength
Ccount
D$size
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' or 'count' instead of '$size' causes errors.
Omitting the dollar sign before 'size'.
2fill in blank
medium

Complete 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'
Alength
B$size
ClengthOf
Dcount
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.
3fill in blank
hard

Fix 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'
A$size
Blength
Ccount
DlengthOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using $length or $count which are invalid operators.
Missing the dollar sign before 'size'.
4fill in blank
hard

Fill 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'
A$size
B$gt
C$lt
D$eq
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt instead of $gt for greater than comparison.
Using $eq which checks equality, not greater than.
5fill in blank
hard

Fill 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'
A$size
Ccount
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.