Skip to content

$allElementsTrue

The $allElementsTrue operator returns true if every element in the input array evaluates to a truthy value.


📌 Syntax

{ "$allElementsTrue": [ <arrayExpression> ] }

✅ Base Example – All True

📥 Input Document

{ "results": [1, true, "ok"] }

📌 Expression

{ "$allElementsTrue": ["$results"] }

📤 Output

true

✅ Base Example – Contains Falsy

📥 Input Document

{ "values": [1, 0, true] }

📌 Expression

{ "$allElementsTrue": ["$values"] }

📤 Output

false

🧱 Ecommerce Example – All Features Enabled

📌 Pipeline

[
  {
    "$project": {
      "name": 1,
      "allEnabled": {
        "$allElementsTrue": {
          "$map": {
            "input": "$features",
            "as": "f",
            "in": "$$f.enabled"
          }
        }
      }
    }
  }
]

📥 Input Document

{
  "name": "Standing Desk",
  "features": [
    { "title": "Electric Height", "enabled": true },
    { "title": "Memory Presets", "enabled": true }
  ]
}

📤 Output

{
  "name": "Standing Desk",
  "allEnabled": true
}

🔧 Common Use Cases

  • Verify that every value meets a condition
  • Check that all features/options are enabled
  • Chain with $map, $filter

  • $anyElementTrue, $reduce, $cond, $and

🧠 Notes

  • Returns true for an empty array
  • Each element is evaluated using JavaScript-style truthiness