0
0
MongoDBquery~10 mins

Conditional expressions ($cond, $switch) in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Conditional expressions ($cond, $switch)
Start
$cond or $switch
Evaluate condition
Return ifTrue
Output value
The $cond or $switch expression evaluates conditions and returns values based on which condition is true.
Execution Sample
MongoDB
db.collection.aggregate([
  { $project: {
      status: { $cond: { if: { $gte: ["$score", 60] }, then: "Pass", else: "Fail" } }
    }
  }
])
This query adds a 'status' field that shows 'Pass' if score is 60 or more, otherwise 'Fail'.
Execution Table
StepInput DocumentCondition ($gte score,60)Condition ResultValue ReturnedOutput Document
1{"name":"Alice","score":85}85 >= 60True"Pass"{"name":"Alice","score":85,"status":"Pass"}
2{"name":"Bob","score":55}55 >= 60False"Fail"{"name":"Bob","score":55,"status":"Fail"}
3{"name":"Carol","score":60}60 >= 60True"Pass"{"name":"Carol","score":60,"status":"Pass"}
4No more documents---Aggregation ends
💡 All documents processed, aggregation completes.
Variable Tracker
VariableStartAfter 1After 2After 3Final
score-855560-
conditionResult-TrueFalseTrue-
status-"Pass""Fail""Pass"-
Key Moments - 3 Insights
Why does the condition use $gte and not just a simple comparison?
MongoDB aggregation expressions require operators like $gte to compare values inside documents, as shown in execution_table rows 1-3.
What happens if the condition is false in $cond?
The 'else' value is returned, as seen in execution_table row 2 where condition is false and 'Fail' is returned.
How does $switch differ from $cond?
$switch can check multiple conditions in order, returning the first true case, unlike $cond which checks only one condition (not shown in this table but conceptually similar).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'status' value for the document with score 55?
Anull
B"Pass"
C"Fail"
D"60"
💡 Hint
Check execution_table row 2 under 'Value Returned' and 'Output Document'.
At which step does the condition evaluate to True for the first time?
AStep 1
BStep 2
CStep 3
DNo step
💡 Hint
Look at the 'Condition Result' column in execution_table rows 1-3.
If the condition was changed to $gt (greater than) 60, what would be the status for score 60?
A"Pass"
B"Fail"
Cnull
DError
💡 Hint
Refer to variable_tracker for score 60 and condition logic; 60 > 60 is false.
Concept Snapshot
$cond syntax: { $cond: { if: <condition>, then: <trueValue>, else: <falseValue> } }
Evaluates condition; returns then if true, else if false.
$switch syntax: { $switch: { branches: [ { case: <cond>, then: <val> }, ... ], default: <val> } }
Checks multiple cases in order; returns first true then value or default.
Used in aggregation pipelines to create conditional fields.
Full Transcript
This visual execution trace shows how MongoDB's conditional expressions $cond and $switch work inside aggregation pipelines. The $cond expression evaluates a condition for each document, such as whether a score is greater than or equal to 60. If true, it returns one value; if false, another. The execution table walks through example documents with scores 85, 55, and 60, showing how the condition is evaluated and what output is produced. Variables like score, conditionResult, and status change as each document is processed. Key moments clarify why MongoDB uses operators like $gte and how $cond returns values based on condition results. The quiz tests understanding by asking about specific steps and outcomes. The snapshot summarizes the syntax and behavior of $cond and $switch for quick reference.