Boolean and Control Flow Operators

Logic and branching — combine conditions and make decisions.

Previous | Index | Next: 12 - Array Operators


Boolean Operators

Boolean operators combine or invert conditions.

$and and $or

Combine multiple boolean expressions.

{ $and: [<expr1>, <expr2>, ...] }   // true if ALL are true
{ $or:  [<expr1>, <expr2>, ...] }   // true if ANY is true

Example: Match projects by funding and state

db.projects.aggregate([
  {
    $addFields: {
      projectFunding: { $sum: "$fundings.amount" }
    }
  },
  {
    $match: {
      $expr: {
        $and: [
          { $eq: ["$projectFunding", 5000] },
          { $eq: ["$state", "END"] }
        ]
      }
    }
  },
  { $out: "projectReport" }
]);

$not

Inverts a boolean expression.

{ $not: [<expression>] }

Example: Set verified to the inverse of created

db.projects.aggregate([
  {
    $addFields: {
      verified: { $not: ["$created"] }
    }
  }
]);

If created is truthy, verified becomes false, and vice versa.


Control Flow Operators

Control flow operators let you apply conditional logic within expressions — like if/else and switch/case in programming.

$cond — If / Then / Else

The $cond operator evaluates a condition and returns one of two values.

Object form (recommended for readability):

{
  $cond: {
    if:   <boolean-expression>,
    then: <value-if-true>,
    else: <value-if-false>
  }
}

Array shorthand:

{ $cond: [<boolean-expression>, <true-case>, <false-case>] }

Example: Set verified based on project state

db.projects.aggregate([
  {
    $addFields: {
      verified: {
        $cond: {
          if:   { $eq: ["$state", "APPROVED"] },
          then: true,
          else: false
        }
      }
    }
  }
]);

Same example in shorthand form

db.projects.aggregate([
  {
    $addFields: {
      verified: {
        $cond: [
          { $eq: ["$state", "APPROVED"] },
          true,
          false
        ]
      }
    }
  },
  {
    $project: {
      title: 1, type: 1, state: 1, verified: 1
    }
  }
]);

$switch — Multi-Branch Conditions

For more than two branches, use $switch — MongoDB’s equivalent of a switch/case statement.

{
  $switch: {
    branches: [
      { case: <expression>, then: <expression> },
      { case: <expression>, then: <expression> },
      ...
    ],
    default: <expression>
  }
}

Example: Assign a numeric value based on project type

db.projects.aggregate([
  {
    $addFields: {
      projectValue: {
        $switch: {
          branches: [
            {
              case: { $eq: ["$type", "REQUEST_PROJECT"] },
              then: 10
            },
            {
              case: { $eq: ["$type", "RESEARCH_PROJECT"] },
              then: 20
            },
            {
              case: { $eq: ["$type", "MANAGEMENT_PROJECT"] },
              then: 5
            }
          ],
          default: 0
        }
      }
    }
  }
]);

When to use $switch vs $cond:

  • 2 branches → use $cond
  • 3+ branches → use $switch for clarity

Decision Flow Diagram

              ┌────────-─┐
              │ $cond    │
              └────┬─────┘
                   │
           ┌───────┴───────┐
      condition true?   condition false?
           │               │
      ┌────▼────┐      ┌───▼─────┐
      │  then   │      │  else   │
      └─────────┘      └─────────┘


              ┌──────────┐
              │ $switch  │
              └────┬─────┘
                   │
        ┌──────────┼─────────-----─┐──── ...
   case 1 true?  case 2 true?  no match?
        │          │               │
   ┌────▼──┐   ┌───▼───┐      ┌────▼────┐
   │ then1 │   │ then2 │      │ default │
   └───────┘   └───────┘      └─────────┘

Next: 12 - Array Operators