0
0
MongoDBquery~10 mins

Conditional expressions ($cond, $switch) 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 use $cond to check if the field 'score' is greater than 50.

MongoDB
{ $cond: { if: { $gt: ["$score", [1]] }, then: "Pass", else: "Fail" } }
Drag options to blanks, or click blank then click option'
A50
B100
C0
D75
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value other than 50 for the comparison.
Confusing $gt with $lt.
2fill in blank
medium

Complete the $cond expression to return 'High' if 'age' is at least 18, otherwise 'Low'.

MongoDB
{ $cond: { if: { $gte: ["$age", [1]] }, then: "High", else: "Low" } }
Drag options to blanks, or click blank then click option'
A18
B16
C20
D21
Attempts:
3 left
💡 Hint
Common Mistakes
Using 21 instead of 18.
Using $gt instead of $gte.
3fill in blank
hard

Fix the error in the $switch expression to correctly return 'Child', 'Teen', or 'Adult' based on 'age'.

MongoDB
{ $switch: { branches: [ { case: { $lt: ["$age", [1]] }, then: "Child" }, { case: { $lt: ["$age", 20] }, then: "Teen" } ], default: "Adult" } }
Drag options to blanks, or click blank then click option'
A21
B13
C18
D16
Attempts:
3 left
💡 Hint
Common Mistakes
Using 13 or 16 instead of 18 for the first case.
Not ordering cases properly.
4fill in blank
hard

Fill both blanks to create a $cond expression that returns 'Adult' if age is 18 or more, else 'Minor'.

MongoDB
{ $cond: { if: { $[1]: ["$age", [2]] }, then: "Adult", else: "Minor" } }
Drag options to blanks, or click blank then click option'
Agte
Blt
C18
D21
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt instead of $gte.
Using 21 instead of 18.
5fill in blank
hard

Fill all three blanks to create a $switch expression that returns 'Low', 'Medium', or 'High' based on 'score'.

MongoDB
{ $switch: { branches: [ { case: { $lt: ["$score", [1]] }, then: "Low" }, { case: { $lt: ["$score", [2]] }, then: "Medium" } ], default: [3] } }
Drag options to blanks, or click blank then click option'
A50
B80
C"High"
D"Medium"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of thresholds.
Using incorrect default value.