0
0
MongoDBquery~20 mins

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

Choose your learning style9 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.