Skip to content

$anyElementTrue

The $anyElementTrue operator returns true if any element of the input array evaluates to a truthy value.


📌 Syntax

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

✅ Base Example – Check If Any Truthy

📥 Input Document

{ "flags": [false, false, true, false] }

📌 Expression

{ "$anyElementTrue": ["$flags"] }

📤 Output

true

✅ Base Example – All Falsy

📥 Input Document

{ "checks": [false, 0, null] }

📌 Expression

{ "$anyElementTrue": ["$checks"] }

📤 Output

false

🧱 Ecommerce Example – Check for Available Features

📌 Pipeline

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

📥 Input Document

{
  "name": "Chair",
  "features": [
    { "title": "Padded Seat", "enabled": false },
    { "title": "Adjustable Height", "enabled": true }
  ]
}

📤 Output

{
  "name": "Chair",
  "hasFeature": true
}

🔧 Common Use Cases

  • Detect at least one match
  • Validate presence of flags or statuses
  • Use after $map or $filter

  • $allElementsTrue, $map, $reduce, $cond, $or

🧠 Notes

  • Returns false for an empty array.
  • Converts each element to boolean using JavaScript-like truthiness.