|
| 1 | +--- |
| 2 | +title: $allElementsTrue |
| 3 | +description: The $allElementsTrue operator returns true if all elements in an array evaluate to true. |
| 4 | +type: operators |
| 5 | +category: set-expression |
| 6 | +--- |
| 7 | + |
| 8 | +# $allElementsTrue |
| 9 | + |
| 10 | +The `$allElementsTrue` operator evaluates an array as a set. It returns `true` if no element in the array has a value of `false` or equivalent to `false` (like `null`, `0`, or `undefined`). If any element evaluates to a value of `false` or the equivalent, the operator returns `false`. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```javascript |
| 15 | +{ |
| 16 | + $allElementsTrue: [ <array> ] |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +## Parameters |
| 21 | + |
| 22 | +| Parameter | Description | |
| 23 | +| --- | --- | |
| 24 | +| `array` | An array of expressions to evaluate. If the array is empty, `$allElementsTrue` returns `true`. | |
| 25 | + |
| 26 | +## Examples |
| 27 | + |
| 28 | +Consider this sample document from the stores collection. |
| 29 | + |
| 30 | +```json |
| 31 | +{ |
| 32 | + "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", |
| 33 | + "name": "First Up Consultants | Beverage Shop - Satterfieldmouth", |
| 34 | + "location": { |
| 35 | + "lat": -89.2384, |
| 36 | + "lon": -46.4012 |
| 37 | + }, |
| 38 | + "staff": { |
| 39 | + "totalStaff": { |
| 40 | + "fullTime": 8, |
| 41 | + "partTime": 20 |
| 42 | + } |
| 43 | + }, |
| 44 | + "sales": { |
| 45 | + "totalSales": 75670, |
| 46 | + "salesByCategory": [ |
| 47 | + { |
| 48 | + "categoryName": "Wine Accessories", |
| 49 | + "totalSales": 34440 |
| 50 | + }, |
| 51 | + { |
| 52 | + "categoryName": "Bitters", |
| 53 | + "totalSales": 39496 |
| 54 | + }, |
| 55 | + { |
| 56 | + "categoryName": "Rum", |
| 57 | + "totalSales": 1734 |
| 58 | + } |
| 59 | + ] |
| 60 | + }, |
| 61 | + "promotionEvents": [ |
| 62 | + { |
| 63 | + "eventName": "Unbeatable Bargain Bash", |
| 64 | + "promotionalDates": { |
| 65 | + "startDate": { |
| 66 | + "Year": 2024, |
| 67 | + "Month": 6, |
| 68 | + "Day": 23 |
| 69 | + }, |
| 70 | + "endDate": { |
| 71 | + "Year": 2024, |
| 72 | + "Month": 7, |
| 73 | + "Day": 2 |
| 74 | + } |
| 75 | + }, |
| 76 | + "discounts": [ |
| 77 | + { |
| 78 | + "categoryName": "Whiskey", |
| 79 | + "discountPercentage": 7 |
| 80 | + }, |
| 81 | + { |
| 82 | + "categoryName": "Bitters", |
| 83 | + "discountPercentage": 15 |
| 84 | + }, |
| 85 | + { |
| 86 | + "categoryName": "Brandy", |
| 87 | + "discountPercentage": 8 |
| 88 | + }, |
| 89 | + { |
| 90 | + "categoryName": "Sports Drinks", |
| 91 | + "discountPercentage": 22 |
| 92 | + }, |
| 93 | + { |
| 94 | + "categoryName": "Vodka", |
| 95 | + "discountPercentage": 19 |
| 96 | + } |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "eventName": "Steal of a Deal Days", |
| 101 | + "promotionalDates": { |
| 102 | + "startDate": { |
| 103 | + "Year": 2024, |
| 104 | + "Month": 9, |
| 105 | + "Day": 21 |
| 106 | + }, |
| 107 | + "endDate": { |
| 108 | + "Year": 2024, |
| 109 | + "Month": 9, |
| 110 | + "Day": 29 |
| 111 | + } |
| 112 | + }, |
| 113 | + "discounts": [ |
| 114 | + { |
| 115 | + "categoryName": "Organic Wine", |
| 116 | + "discountPercentage": 19 |
| 117 | + }, |
| 118 | + { |
| 119 | + "categoryName": "White Wine", |
| 120 | + "discountPercentage": 20 |
| 121 | + }, |
| 122 | + { |
| 123 | + "categoryName": "Sparkling Wine", |
| 124 | + "discountPercentage": 19 |
| 125 | + }, |
| 126 | + { |
| 127 | + "categoryName": "Whiskey", |
| 128 | + "discountPercentage": 17 |
| 129 | + }, |
| 130 | + { |
| 131 | + "categoryName": "Vodka", |
| 132 | + "discountPercentage": 23 |
| 133 | + } |
| 134 | + ] |
| 135 | + } |
| 136 | + ] |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +### Example 1: Determine if all the discount percentages are higher than zero |
| 141 | + |
| 142 | +This query determines if all the discount percentages in each promotion event are greater than zero. |
| 143 | + |
| 144 | +```javascript |
| 145 | +db.stores.aggregate([ |
| 146 | + { $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} }, |
| 147 | + { $unwind: "$promotionEvents" }, |
| 148 | + { |
| 149 | + $project: { |
| 150 | + allDiscountsValid: { |
| 151 | + $allElementsTrue: [ |
| 152 | + { |
| 153 | + $map: { |
| 154 | + input: "$promotionEvents.discounts.discountPercentage", |
| 155 | + as: "discount", |
| 156 | + in: { $gt: ["$$discount", 0] } |
| 157 | + } |
| 158 | + } |
| 159 | + ] |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +]) |
| 164 | +``` |
| 165 | + |
| 166 | +This query returns the following result. |
| 167 | + |
| 168 | +```json |
| 169 | +[ |
| 170 | + { |
| 171 | + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", |
| 172 | + "allDiscountsValid": true |
| 173 | + } |
| 174 | +] |
| 175 | +``` |
0 commit comments