|
| 1 | +--- |
| 2 | +title: $and |
| 3 | +description: The $and operator joins multiple query clauses and returns documents that match all specified conditions. |
| 4 | +type: operators |
| 5 | +category: logical-query |
| 6 | +--- |
| 7 | + |
| 8 | +# $and |
| 9 | + |
| 10 | +The `$and` operator performs a logical AND operation on an array of expressions and retrieves documents that satisfy all the expressions. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```javascript |
| 15 | +{ |
| 16 | + $and: [{ |
| 17 | + < expression1 > |
| 18 | + }, { |
| 19 | + < expression2 > |
| 20 | + }, ..., { |
| 21 | + < expressionN > |
| 22 | + }] |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## Parameters |
| 27 | + |
| 28 | +| Parameter | Description | |
| 29 | +|-----------|-------------| |
| 30 | +| `expression` | An array of expressions that must all be true for a document to be included in the results | |
| 31 | + |
| 32 | +## Examples |
| 33 | + |
| 34 | +Consider this sample document from the stores collection. |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", |
| 39 | + "name": "First Up Consultants | Beverage Shop - Satterfieldmouth", |
| 40 | + "location": { |
| 41 | + "lat": -89.2384, |
| 42 | + "lon": -46.4012 |
| 43 | + }, |
| 44 | + "staff": { |
| 45 | + "totalStaff": { |
| 46 | + "fullTime": 8, |
| 47 | + "partTime": 20 |
| 48 | + } |
| 49 | + }, |
| 50 | + "sales": { |
| 51 | + "totalSales": 75670, |
| 52 | + "salesByCategory": [ |
| 53 | + { |
| 54 | + "categoryName": "Wine Accessories", |
| 55 | + "totalSales": 34440 |
| 56 | + }, |
| 57 | + { |
| 58 | + "categoryName": "Bitters", |
| 59 | + "totalSales": 39496 |
| 60 | + }, |
| 61 | + { |
| 62 | + "categoryName": "Rum", |
| 63 | + "totalSales": 1734 |
| 64 | + } |
| 65 | + ] |
| 66 | + }, |
| 67 | + "promotionEvents": [ |
| 68 | + { |
| 69 | + "eventName": "Unbeatable Bargain Bash", |
| 70 | + "promotionalDates": { |
| 71 | + "startDate": { |
| 72 | + "Year": 2024, |
| 73 | + "Month": 6, |
| 74 | + "Day": 23 |
| 75 | + }, |
| 76 | + "endDate": { |
| 77 | + "Year": 2024, |
| 78 | + "Month": 7, |
| 79 | + "Day": 2 |
| 80 | + } |
| 81 | + }, |
| 82 | + "discounts": [ |
| 83 | + { |
| 84 | + "categoryName": "Whiskey", |
| 85 | + "discountPercentage": 7 |
| 86 | + }, |
| 87 | + { |
| 88 | + "categoryName": "Bitters", |
| 89 | + "discountPercentage": 15 |
| 90 | + }, |
| 91 | + { |
| 92 | + "categoryName": "Brandy", |
| 93 | + "discountPercentage": 8 |
| 94 | + }, |
| 95 | + { |
| 96 | + "categoryName": "Sports Drinks", |
| 97 | + "discountPercentage": 22 |
| 98 | + }, |
| 99 | + { |
| 100 | + "categoryName": "Vodka", |
| 101 | + "discountPercentage": 19 |
| 102 | + } |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "eventName": "Steal of a Deal Days", |
| 107 | + "promotionalDates": { |
| 108 | + "startDate": { |
| 109 | + "Year": 2024, |
| 110 | + "Month": 9, |
| 111 | + "Day": 21 |
| 112 | + }, |
| 113 | + "endDate": { |
| 114 | + "Year": 2024, |
| 115 | + "Month": 9, |
| 116 | + "Day": 29 |
| 117 | + } |
| 118 | + }, |
| 119 | + "discounts": [ |
| 120 | + { |
| 121 | + "categoryName": "Organic Wine", |
| 122 | + "discountPercentage": 19 |
| 123 | + }, |
| 124 | + { |
| 125 | + "categoryName": "White Wine", |
| 126 | + "discountPercentage": 20 |
| 127 | + }, |
| 128 | + { |
| 129 | + "categoryName": "Sparkling Wine", |
| 130 | + "discountPercentage": 19 |
| 131 | + }, |
| 132 | + { |
| 133 | + "categoryName": "Whiskey", |
| 134 | + "discountPercentage": 17 |
| 135 | + }, |
| 136 | + { |
| 137 | + "categoryName": "Vodka", |
| 138 | + "discountPercentage": 23 |
| 139 | + } |
| 140 | + ] |
| 141 | + } |
| 142 | + ] |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### Example 1: Use AND operator as logical-query |
| 147 | + |
| 148 | +This query filters for stores where the number of full-time employees is greater than 10 and part-time employees is less than 15 using the `$and` operator. It projects only the `name` and `staff` fields and limits the result to three records. |
| 149 | + |
| 150 | +```javascript |
| 151 | +db.stores.find({ |
| 152 | + $and: [{ |
| 153 | + "staff.employeeCount.fullTime": { |
| 154 | + $gt: 10 |
| 155 | + } |
| 156 | + }, { |
| 157 | + "staff.employeeCount.partTime": { |
| 158 | + $lt: 15 |
| 159 | + } |
| 160 | + }] |
| 161 | +}, { |
| 162 | + "name": 1, |
| 163 | + "staff": 1 |
| 164 | +}).limit(3) |
| 165 | +``` |
| 166 | + |
| 167 | +The first three results returned by this query are: |
| 168 | + |
| 169 | +```json |
| 170 | +[ |
| 171 | + { |
| 172 | + "_id": "e60c807b-d31c-4903-befb-5d608f260ba3", |
| 173 | + "name": "Wide World Importers | Appliance Emporium - Craigfort", |
| 174 | + "staff": { |
| 175 | + "totalStaff": { |
| 176 | + "fullTime": 11, |
| 177 | + "partTime": 8 |
| 178 | + } |
| 179 | + } |
| 180 | + }, |
| 181 | + { |
| 182 | + "_id": "70032165-fded-47b4-84a3-8d9c18a4d1e7", |
| 183 | + "name": "Northwind Traders | Picture Frame Bazaar - Lake Joesph", |
| 184 | + "staff": { |
| 185 | + "totalStaff": { |
| 186 | + "fullTime": 14, |
| 187 | + "partTime": 0 |
| 188 | + } |
| 189 | + } |
| 190 | + }, |
| 191 | + { |
| 192 | + "_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3", |
| 193 | + "name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview", |
| 194 | + "staff": { |
| 195 | + "totalStaff": { |
| 196 | + "fullTime": 16, |
| 197 | + "partTime": 8 |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +] |
| 202 | +``` |
| 203 | + |
| 204 | +### Example 2: Use AND operator as boolean-expression to find stores with high sales and sufficient staff |
| 205 | + |
| 206 | +This query finds stores that have both total sales greater than 100,000 and more than 30 total staff members. |
| 207 | + |
| 208 | +```javascript |
| 209 | +db.stores.aggregate([ |
| 210 | + { |
| 211 | + $project: { |
| 212 | + name: 1, |
| 213 | + totalSales: "$sales.totalSales", |
| 214 | + totalStaff: { |
| 215 | + $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] |
| 216 | + }, |
| 217 | + meetsHighPerformanceCriteria: { |
| 218 | + $and: [ |
| 219 | + { $gt: ["$sales.totalSales", 100000] }, |
| 220 | + { $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 30] } |
| 221 | + ] |
| 222 | + } |
| 223 | + } |
| 224 | + }, |
| 225 | + { $limit: 2 } |
| 226 | +]) |
| 227 | +``` |
| 228 | + |
| 229 | +The first two results returned by this query are: |
| 230 | + |
| 231 | +```json |
| 232 | +[ |
| 233 | + { |
| 234 | + "_id": "905d1939-e03a-413e-a9c4-221f74055aac", |
| 235 | + "name": "Trey Research | Home Office Depot - Lake Freeda", |
| 236 | + "totalStaff": 31, |
| 237 | + "meetsHighPerformanceCriteria": false |
| 238 | + }, |
| 239 | + { |
| 240 | + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", |
| 241 | + "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort", |
| 242 | + "totalStaff": 27, |
| 243 | + "meetsHighPerformanceCriteria": false |
| 244 | + } |
| 245 | +] |
| 246 | +``` |
0 commit comments