$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
$mapor$filter
🔗 Related Operators
$allElementsTrue,$map,$reduce,$cond,$or
🧠Notes
- Returns
falsefor an empty array. - Converts each element to boolean using JavaScript-like truthiness.