Operators

Conditional operators

Operators used to perform conditional operations.


$cond

Evaluates a boolean expression to return one of the two specified return expressions.

{ "$cond": { "if": "boolean", "then": "any", "else": "any" } }
expry({ $cond: { if: true, then: "hello", else: "bye" } }); // hello
expry({ $cond: { if: false, then: "hello", else: "bye" } }); // bye

$ifNull

Evaluates input expressions for null values and returns:

  • The first non-null input expression value found.
  • A replacement expression value if all input expressions evaluate to null.
{ "$ifNull": ["any", "any", "...", "replacement"] }
expry({ $ifNull: [null, "hello", "bye"] }); // hello
expry({ $ifNull: [null, null, "bye"] }); // bye
expry({ $ifNull: [null, null, null] }); // null

$switch

Evaluates a series of case expressions. When it finds an expression which evaluates to true, it executes a specified expression and breaks out of the control flow.

{
  "$switch": {
    "branches": [
      { "case": "boolean", "then": "any" },
      { "case": "boolean", "then": "any" }
      "..."
    ],
    "default": "any"
  }
}
expry({
  $switch: {
    branches: [
      { case: false, then: 1 },
      { case: true, then: 2 },
    ],
    default: 3,
  },
}); // 2
expry({
  $switch: {
    branches: [
      { case: false, then: 1 },
      { case: false, then: 2 },
    ],
    default: 3,
  },
}); // 3