Bird
Raised Fist0
MongoDBquery~20 mins

Array expressions ($size, $arrayElemAt, $filter) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
What is the output of $size on an empty array?
Consider the following MongoDB aggregation pipeline stage:

{ $project: { count: { $size: "$items" } } }

If the document is { items: [] }, what will be the value of count in the output?
MongoDB
{ items: [] }
Anull
B0
C1
Dundefined
Attempts:
2 left
💡 Hint
Think about how many elements are inside the array.
❓ query_result
intermediate
1:30remaining
Using $arrayElemAt to get the second element
Given the document:

{ fruits: ["apple", "banana", "cherry"] }

What is the output of this aggregation projection?

{ $project: { secondFruit: { $arrayElemAt: ["$fruits", 1] } } }
MongoDB
{ fruits: ["apple", "banana", "cherry"] }
A"cherry"
B"apple"
C"banana"
Dnull
Attempts:
2 left
💡 Hint
Remember that array indexes start at 0.
❓ query_result
advanced
2:00remaining
Filtering array elements with $filter
Given the document:

{ scores: [85, 42, 90, 70, 55] }

What is the output of this aggregation projection?

{ $project: { highScores: { $filter: { input: "$scores", as: "score", cond: { $gte: ["$$score", 70] } } } } }
MongoDB
{ scores: [85, 42, 90, 70, 55] }
A[42, 55]
B[]
C[85, 42, 90, 70, 55]
D[85, 90, 70]
Attempts:
2 left
💡 Hint
The condition keeps scores greater than or equal to 70.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in $filter usage
Which of the following $filter expressions will cause a syntax error in MongoDB aggregation?
A{ $filter: { input: "$values", cond: { $gt: ["$$val", 10] } } }
B{ $filter: { input: "$values", as: "val", cond: { $lt: ["$$val", 5] } } }
C{ $filter: { input: "$values", as: "val", cond: { $eq: ["$$val", 3] } } }
D{ $filter: { input: "$values", as: "val", cond: { $gt: ["$$val", 10] } } }
Attempts:
2 left
💡 Hint
Check if all required fields are present.
🧠 Conceptual
expert
3:00remaining
Combining $size, $filter, and $arrayElemAt for complex queries
You have documents with a field tags which is an array of strings. You want to find the first tag that starts with the letter 'a' and also know how many tags start with 'a'. Which aggregation expression correctly produces both the first matching tag and the count of matching tags?
A{ firstTag: { $arrayElemAt: [ { $filter: { input: "$tags", as: "tag", cond: { $regexMatch: { input: "$$tag", regex: /^a/ } } } }, 0 ] }, count: { $size: { $filter: { input: "$tags", as: "tag", cond: { $regexMatch: { input: "$$tag", regex: /^a/ } } } } } }
B{ firstTag: { $arrayElemAt: [ "$tags", 0 ] }, count: { $size: "$tags" } }
C{ firstTag: { $filter: { input: "$tags", as: "tag", cond: { $regexMatch: { input: "$$tag", regex: /^a/ } } } }, count: { $size: "$tags" } }
D{ firstTag: { $arrayElemAt: [ { $filter: { input: "$tags", cond: { $regexMatch: { input: "$$tag", regex: /^a/ } } } }, 0 ] }, count: { $size: { $filter: { input: "$tags", as: "tag", cond: { $regexMatch: { input: "$$tag", regex: /^a/ } } } } } }
Attempts:
2 left
💡 Hint
Remember to define the variable name with 'as' in $filter and use it consistently.

Practice

(1/5)
1. What does the MongoDB expression $size do when used on an array field?
easy
A. Counts how many elements are in the array
B. Returns the first element of the array
C. Filters elements based on a condition
D. Sorts the array elements

Solution

  1. Step 1: Understand the purpose of $size

    The $size operator counts the number of elements inside an array.
  2. Step 2: Compare with other operators

    $arrayElemAt picks an element by position, $filter selects elements by condition, and sorting is unrelated.
  3. Final Answer:

    Counts how many elements are in the array -> Option A
  4. Quick Check:

    $size = count elements [OK]
Hint: Remember $size counts array length, not elements themselves [OK]
Common Mistakes:
  • Confusing $size with $arrayElemAt
  • Thinking $size filters elements
  • Assuming $size sorts arrays
2. Which of the following is the correct syntax to get the third element of an array field named items using MongoDB aggregation?
easy
A. { $arrayElemAt: ["items", 3] }
B. { $size: ["$items", 2] }
C. { $arrayElemAt: ["$items", 2] }
D. { $filter: { input: "$items", as: "item", cond: { $eq: ["$$item", 2] } } }

Solution

  1. Step 1: Recall zero-based indexing in $arrayElemAt

    The third element is at index 2 because counting starts at 0.
  2. Step 2: Check syntax correctness

    The correct syntax uses $arrayElemAt with the array field prefixed by $ and the zero-based index.
  3. Final Answer:

    { $arrayElemAt: ["$items", 2] } -> Option C
  4. Quick Check:

    Third element index = 2 [OK]
Hint: Remember arrays start at 0, so third element is index 2 [OK]
Common Mistakes:
  • Using 1-based index instead of 0-based
  • Missing $ before field name
  • Confusing $size with $arrayElemAt
3. Given the document { scores: [10, 20, 30, 40] }, what will the aggregation expression { $size: "$scores" } return?
medium
A. 4
B. 30
C. [10, 20, 30, 40]
D. Error

Solution

  1. Step 1: Understand what $size does

    $size counts the number of elements in the array.
  2. Step 2: Count elements in the array

    The array [10, 20, 30, 40] has 4 elements.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Array length = 4 [OK]
Hint: Count elements inside brackets for $size result [OK]
Common Mistakes:
  • Returning the third element instead of count
  • Returning the whole array instead of count
  • Assuming syntax error
4. Identify the error in this MongoDB aggregation expression: { $arrayElemAt: ["$values", "1"] }
medium
A. Field name missing $ prefix
B. Index must be a number, not a string
C. Array must be inside an object
D. No error, expression is correct

Solution

  1. Step 1: Check the type of index in $arrayElemAt

    The index must be a number, but here it is a string "1".
  2. Step 2: Confirm correct field referencing

    The field name "$values" is correctly prefixed with $.
  3. Final Answer:

    Index must be a number, not a string -> Option B
  4. Quick Check:

    Index type = number, not string [OK]
Hint: Use number type for index in $arrayElemAt [OK]
Common Mistakes:
  • Using string instead of number for index
  • Omitting $ before field name
  • Thinking array must be wrapped differently
5. You have documents with a field tags as an array of strings. How would you write a MongoDB aggregation expression to get the count of tags that start with the letter 'a'?
hard
A. { $size: "$tags" }
B. { $arrayElemAt: ["$tags", 0] }
C. { $filter: { input: "$tags", as: "tag", cond: { $eq: ["$$tag", "a"] } } }
D. { $size: { $filter: { input: "$tags", as: "tag", cond: { $regexMatch: { input: "$$tag", regex: /^a/ } } } } }

Solution

  1. Step 1: Use $filter to select tags starting with 'a'

    The $filter operator creates a new array with tags matching the regex /^a/.
  2. Step 2: Use $size to count filtered tags

    $size counts how many tags matched the condition.
  3. Final Answer:

    { $size: { $filter: { input: "$tags", as: "tag", cond: { $regexMatch: { input: "$$tag", regex: /^a/ } } } } } -> Option D
  4. Quick Check:

    Filter then count = correct approach [OK]
Hint: Filter first, then count with $size for conditional counts [OK]
Common Mistakes:
  • Using $arrayElemAt instead of $filter
  • Filtering with exact match instead of regex
  • Counting whole array without filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard