$anyElementTrue
Returns true if any element in an array is true, treating null, undefined, and missing values as false. Empty arrays return false.
Syntax
{ $anyElementTrue: [<array_expression>] }
Parameters
array_expression
objectrequiredExpression that resolves to an array of boolean values
Examples
Check any condition true
Verify that at least one boolean value in array is true
Query:
db.surveys.aggregate([{ $project: { anyPassed: { $anyElementTrue: ["$testResults"] } } }])
Output:
Documents with anyPassed field indicating if any test result is true
Check for any valid elements
Determine if any elements meet specific criteria
Query:
db.products.aggregate([{ $project: { hasInStock: { $anyElementTrue: [{ $map: { input: "$items", as: "item", in: { $gt: ["$$item.quantity", 0] } } }] } } }])
Output:
Documents with hasInStock field indicating if any items have positive quantity