$allElementsTrue

Returns true if all elements in an array are true, treating null, undefined, and missing values as false. Empty arrays return true.

Syntax

{ $allElementsTrue: [<array_expression>] }

Parameters

array_expressionobjectrequired

Expression that resolves to an array of boolean values

Examples

Check all conditions true

Verify that all boolean values in array are true

Query:

db.surveys.aggregate([{ $project: { allPassed: { $allElementsTrue: ["$testResults"] } } }])

Output:

Documents with allPassed field indicating if all test results are true

Validate array conditions

Check if all elements meet specific criteria

Query:

db.products.aggregate([{ $project: { allInStock: { $allElementsTrue: [{ $map: { input: "$items", as: "item", in: { $gt: ["$$item.quantity", 0] } } }] } } }])

Output:

Documents with allInStock field indicating if all items have positive quantity

All elements validation

Validate that all array elements pass boolean test

Query:

db.students.aggregate([{ $project: { passedAll: { $allElementsTrue: ["$examResults"] } } }])

Output:

Documents with passedAll field showing if student passed all exams

Related