0
0
MongoDBquery~20 mins

Conditional expressions ($cond, $switch) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Expressions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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" } } } }
A{"score": 85, "status": false}
B{"score": 85, "status": "Fail"}
C{"score": 85, "status": true}
D{"score": 85, "status": "Pass"}
Attempts:
2 left
💡 Hint
Think about whether the score is greater than or equal to 60.
query_result
intermediate
2: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" } } } }
A{"score": 75, "grade": "A"}
B{"score": 75, "grade": "B"}
C{"score": 75, "grade": "C"}
D{"score": 75, "grade": "F"}
Attempts:
2 left
💡 Hint
Check the conditions in order from top to bottom.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in $cond usage
Which option contains a syntax error in the use of the $cond expression in MongoDB aggregation?
A{ $project: { result: { $cond: { condition: { $eq: ["$value", 10] }, then: "Ten", else: "Other" } } } }
B{ $project: { result: { $cond: { if: { $eq: ["$value", 10] }, then: "Ten", else: "Other" } } } }
C{ $project: { result: { $cond: [ { $eq: ["$value", 10] }, "Ten", "Other" ] } } }
D} } } } "rehtO" :esle ,"neT" :neht ,} ]01 ,"eulav$"[ :qe$ { :fi { :dnoc$ { :tluser { :tcejorp$ {
Attempts:
2 left
💡 Hint
Check the correct field name for the condition in $cond.
optimization
advanced
2:00remaining
Optimizing $switch with overlapping conditions
Consider this $switch expression:

{ $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?
AThe $switch should use $cond instead for better performance.
BThe order of branches is wrong; the condition for 90 should come before 70 to avoid always matching 70 first.
CThe default value should be "Pass" instead of "Fail".
DThere is no problem; the $switch works as expected.
Attempts:
2 left
💡 Hint
Think about how $switch evaluates branches in order.
🧠 Conceptual
expert
2: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?
AIf the condition is false, the output field will be null.
BIf the condition is false, the output field will be missing from the document.
CMongoDB throws a syntax error because else is required.
DIf the condition is false, the output field will have the value false.
Attempts:
2 left
💡 Hint
Think about default behavior when else is not specified.