|
| 1 | +type: operator |
| 2 | +category: evaluation-query |
| 3 | +name: $expr |
| 4 | +description: |- |
| 5 | + The $expr operator allows the use of aggregation expressions within the query language, enabling complex field comparisons and calculations. |
| 6 | +summary: |- |
| 7 | + The `$expr` operator allows the use of aggregation expressions within the query language, which enables us to compare fields from the same document, perform calculations, and use aggregation operators in find operations. The `$expr` operator is useful for complex field comparisons that can't be achieved with traditional query operators. |
| 8 | +syntax: |- |
| 9 | + { |
| 10 | + $expr: { <aggregation expression> } |
| 11 | + } |
| 12 | +parameters: |
| 13 | +- name: <aggregation expression> |
| 14 | + required: true |
| 15 | + description: |- |
| 16 | + Any valid aggregation expression that evaluates to a boolean value. The expression includes field comparisons, arithmetic operations, conditional expressions, and other aggregation operators. |
| 17 | +examples: |
| 18 | + sample: >- |
| 19 | + { |
| 20 | + "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", |
| 21 | + "name": "First Up Consultants | Beverage Shop - Satterfieldmouth", |
| 22 | + "location": { |
| 23 | + "lat": -89.2384, |
| 24 | + "lon": -46.4012 |
| 25 | + }, |
| 26 | + "staff": { |
| 27 | + "totalStaff": { |
| 28 | + "fullTime": 8, |
| 29 | + "partTime": 20 |
| 30 | + } |
| 31 | + }, |
| 32 | + "sales": { |
| 33 | + "totalSales": 75670, |
| 34 | + "salesByCategory": [ |
| 35 | + { |
| 36 | + "categoryName": "Wine Accessories", |
| 37 | + "totalSales": 34440 |
| 38 | + }, |
| 39 | + { |
| 40 | + "categoryName": "Bitters", |
| 41 | + "totalSales": 39496 |
| 42 | + }, |
| 43 | + { |
| 44 | + "categoryName": "Rum", |
| 45 | + "totalSales": 1734 |
| 46 | + } |
| 47 | + ] |
| 48 | + }, |
| 49 | + "promotionEvents": [ |
| 50 | + { |
| 51 | + "eventName": "Unbeatable Bargain Bash", |
| 52 | + "promotionalDates": { |
| 53 | + "startDate": { |
| 54 | + "Year": 2024, |
| 55 | + "Month": 6, |
| 56 | + "Day": 23 |
| 57 | + }, |
| 58 | + "endDate": { |
| 59 | + "Year": 2024, |
| 60 | + "Month": 7, |
| 61 | + "Day": 2 |
| 62 | + } |
| 63 | + }, |
| 64 | + "discounts": [ |
| 65 | + { |
| 66 | + "categoryName": "Whiskey", |
| 67 | + "discountPercentage": 7 |
| 68 | + }, |
| 69 | + { |
| 70 | + "categoryName": "Bitters", |
| 71 | + "discountPercentage": 15 |
| 72 | + }, |
| 73 | + { |
| 74 | + "categoryName": "Brandy", |
| 75 | + "discountPercentage": 8 |
| 76 | + }, |
| 77 | + { |
| 78 | + "categoryName": "Sports Drinks", |
| 79 | + "discountPercentage": 22 |
| 80 | + }, |
| 81 | + { |
| 82 | + "categoryName": "Vodka", |
| 83 | + "discountPercentage": 19 |
| 84 | + } |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "eventName": "Steal of a Deal Days", |
| 89 | + "promotionalDates": { |
| 90 | + "startDate": { |
| 91 | + "Year": 2024, |
| 92 | + "Month": 9, |
| 93 | + "Day": 21 |
| 94 | + }, |
| 95 | + "endDate": { |
| 96 | + "Year": 2024, |
| 97 | + "Month": 9, |
| 98 | + "Day": 29 |
| 99 | + } |
| 100 | + }, |
| 101 | + "discounts": [ |
| 102 | + { |
| 103 | + "categoryName": "Organic Wine", |
| 104 | + "discountPercentage": 19 |
| 105 | + }, |
| 106 | + { |
| 107 | + "categoryName": "White Wine", |
| 108 | + "discountPercentage": 20 |
| 109 | + }, |
| 110 | + { |
| 111 | + "categoryName": "Sparkling Wine", |
| 112 | + "discountPercentage": 19 |
| 113 | + }, |
| 114 | + { |
| 115 | + "categoryName": "Whiskey", |
| 116 | + "discountPercentage": 17 |
| 117 | + }, |
| 118 | + { |
| 119 | + "categoryName": "Vodka", |
| 120 | + "discountPercentage": 23 |
| 121 | + } |
| 122 | + ] |
| 123 | + } |
| 124 | + ] |
| 125 | + } |
| 126 | + items: |
| 127 | + - title: Compare full-time and part-time staff |
| 128 | + description: |- |
| 129 | + The example retrieves stores with the number of part-time employees greater than full-time employees. |
| 130 | + query: |- |
| 131 | + db.stores.find({_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", |
| 132 | + $expr: { |
| 133 | + $gt: ["$staff.employeeCount.partTime", "$staff.employeeCount.fullTime"] |
| 134 | + } |
| 135 | + }) |
| 136 | + output: |
| 137 | + value: |- |
| 138 | + { |
| 139 | + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", |
| 140 | + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", |
| 141 | + "location": { |
| 142 | + "lat": 70.1272, |
| 143 | + "lon": 69.7296 |
| 144 | + }, |
| 145 | + "staff": { |
| 146 | + "contractorCount": 5, |
| 147 | + "employeeCount": { "fullTime": 19, "partTime": 20 } |
| 148 | + }, |
| 149 | + "sales": { |
| 150 | + "totalSales": 151864, |
| 151 | + "salesByCategory": [ |
| 152 | + { |
| 153 | + "categoryName": "Sound Bars", |
| 154 | + "totalSales": 2120 |
| 155 | + }, |
| 156 | + { |
| 157 | + "categoryName": "Home Theater Projectors", |
| 158 | + "totalSales": 45004 |
| 159 | + } |
| 160 | + }, |
| 161 | + "storeOpeningDate": ISODate("2024-09-23T13:45:01.480Z"), |
| 162 | + "lastUpdated": ISODate("2025-06-11T11:06:57.922Z"), |
| 163 | + "status": "active", |
| 164 | + "category": "high-volume", |
| 165 | + "priority": 1, |
| 166 | + "reviewDate": ISODate("2025-06-11T11:10:50.276Z") |
| 167 | + } |
| 168 | + ] |
| 169 | + - title: Conditional logic with store location |
| 170 | + description: |- |
| 171 | + The example demonstrates the conditional logic usage with `$expr` pulling stores in the southern hemisphere where the staff efficiency ratio (sales per employee) exceeds 2000. |
| 172 | + query: |- |
| 173 | + db.stores.find({_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", |
| 174 | + $expr: { |
| 175 | + $and: [ |
| 176 | + { $gte: ["$location.lat", 70.1272] }, |
| 177 | + { |
| 178 | + $gt: [ |
| 179 | + { |
| 180 | + $divide: [ |
| 181 | + "$sales.totalSales", |
| 182 | + { $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] } |
| 183 | + ] |
| 184 | + }, |
| 185 | + 2000 |
| 186 | + ] |
| 187 | + } |
| 188 | + ] |
| 189 | + } |
| 190 | + }).limit(1) |
| 191 | + output: |
| 192 | + value: |- |
| 193 | + [ |
| 194 | + { |
| 195 | + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", |
| 196 | + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", |
| 197 | + "location": { |
| 198 | + "lat": 70.1272, |
| 199 | + "lon": 69.7296 |
| 200 | + }, |
| 201 | + "staff": { |
| 202 | + "totalStaff": { |
| 203 | + "fullTime": 19, |
| 204 | + "partTime": 20 |
| 205 | + } |
| 206 | + }, |
| 207 | + "sales": { |
| 208 | + "totalSales": 151864, |
| 209 | + "salesByCategory": [ |
| 210 | + { |
| 211 | + "categoryName": "Sound Bars", |
| 212 | + "totalSales": 2120 |
| 213 | + }, |
| 214 | + { |
| 215 | + "categoryName": "Home Theater Projectors", |
| 216 | + "totalSales": 45004 |
| 217 | + } |
| 218 | + ] |
| 219 | + }, |
| 220 | + "storeOpeningDate": ISODate("2024-09-23T13:45:01.480Z"), |
| 221 | + "lastUpdated": ISODate("2025-06-11T11:06:57.922Z"), |
| 222 | + "status": "active", |
| 223 | + "category": "high-volume", |
| 224 | + "priority": 1, |
| 225 | + "reviewDate": ISODate("2025-06-11T11:10:50.276Z") |
| 226 | + } |
| 227 | + ] |
| 228 | +related: [] |
0 commit comments