Challenge - 5 Problems
Conditional Expressions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of $cond in aggregation
Given the following MongoDB aggregation pipeline stage, what will be the value of the field
status in the output document?{ $project: { score: 85, status: { $cond: { if: { $gte: ["$score", 60] }, then: "Pass", else: "Fail" } } } }Attempts:
2 left
💡 Hint
Think about whether the score is greater than or equal to 60.
✗ Incorrect
The $cond expression checks if score is >= 60. Since 85 >= 60 is true, it returns "Pass".
❓ query_result
intermediate2:00remaining
Using $switch for multiple conditions
What will be the value of the field
grade after applying this aggregation stage?{ $project: { score: 75, grade: { $switch: { branches: [ { case: { $gte: ["$score", 90] }, then: "A" }, { case: { $gte: ["$score", 80] }, then: "B" }, { case: { $gte: ["$score", 70] }, then: "C" } ], default: "F" } } } }Attempts:
2 left
💡 Hint
Check the conditions in order from top to bottom.
✗ Incorrect
The $switch checks each case in order. 75 is not >= 90 or 80, but it is >= 70, so grade is "C".
📝 Syntax
advanced2:00remaining
Identify the syntax error in $cond usage
Which option contains a syntax error in the use of the $cond expression in MongoDB aggregation?
Attempts:
2 left
💡 Hint
Check the correct field name for the condition in $cond.
✗ Incorrect
The $cond operator requires the field 'if' for the condition, not 'condition'. Option A uses 'condition' which is invalid syntax.
❓ optimization
advanced2:00remaining
Optimizing $switch with overlapping conditions
Consider this $switch expression:
What is the problem with this $switch and how to fix it?
{ $switch: { branches: [ { case: { $gte: ["$score", 70] }, then: "Pass" }, { case: { $gte: ["$score", 90] }, then: "Excellent" } ], default: "Fail" } }What is the problem with this $switch and how to fix it?
Attempts:
2 left
💡 Hint
Think about how $switch evaluates branches in order.
✗ Incorrect
Since $switch checks branches in order, the first condition ($gte: 70) matches all scores >= 70, including those >= 90, so the second branch is never reached. Reordering fixes this.
🧠 Conceptual
expert2:00remaining
Behavior of $cond with missing else branch
What happens if you use $cond with only
if and then fields, but omit the else field in MongoDB aggregation?Attempts:
2 left
💡 Hint
Think about default behavior when else is not specified.
✗ Incorrect
When else is omitted, $cond returns null if the condition is false.