Skip to main content

$ifelse

The $ifelse operator evaluates a condition and returns one of two values based on the result.

Input

  • An array containing exactly three values: the condition to evaluate, the value to return if true, and the value to return if false

Output

  • The second value if the condition is truthy, the third value otherwise

Examples

Basic Conditional

{
  "$ifelse": [
    {
      "$gt": [
        {
          "$input": "score"
        },
        90
      ]
    },
    "A",
    "B"
  ]
}
Result: "A" if score is greater than 90, otherwise "B"

Conditional with Field Access

{
  "$ifelse": [
    {
      "$eq": [
        {
          "$input": "status"
        },
        "active"
      ]
    },
    {
      "$input": "active_date"
    },
    {
      "$input": "inactive_date"
    }
  ]
}
Result: The active_date field if status is “active”, otherwise the inactive_date field