|
| 1 | +--- |
| 2 | +title: Aggregate |
| 3 | +description: The aggregate command is used to process data records and return computed results. |
| 4 | +type: commands |
| 5 | +category: aggregation |
| 6 | +--- |
| 7 | + |
| 8 | +# aggregate |
| 9 | + |
| 10 | +The `aggregate` command is used to process data records and return computed results. It performs operations on the data, such as filtering, grouping, and sorting, and can transform the data in various ways. The `aggregate` command is highly versatile and is commonly used for data analysis and reporting. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```console |
| 15 | +db.collection.aggregate(pipeline, options) |
| 16 | +``` |
| 17 | + |
| 18 | +- **pipeline**: An array of aggregation stages that process and transform the data. |
| 19 | +- **options**: Optional. Specifies more options for the aggregation, such as `explain`, `allowDiskUse`, and `cursor`. |
| 20 | + |
| 21 | +## Examples |
| 22 | + |
| 23 | +### Example 1: Calculate total sales by category |
| 24 | + |
| 25 | +This example demonstrates how to calculate the total sales for each category in the `stores` collection. |
| 26 | + |
| 27 | +```javascript |
| 28 | +db.stores.aggregate([ |
| 29 | + { |
| 30 | + $unwind: "$sales.salesByCategory" |
| 31 | + }, |
| 32 | + { |
| 33 | + $group: { |
| 34 | + _id: "$sales.salesByCategory.categoryName", |
| 35 | + totalSales: { $sum: "$sales.salesByCategory.totalSales" } |
| 36 | + } |
| 37 | + } |
| 38 | +]) |
| 39 | +``` |
| 40 | + |
| 41 | +#### Sample output |
| 42 | + |
| 43 | +```javascript |
| 44 | +[mongos] StoreData> db.stores.aggregate([ |
| 45 | +... { |
| 46 | +... $unwind: "$sales.salesByCategory" |
| 47 | +... }, |
| 48 | +... { |
| 49 | +... $group: { |
| 50 | +... _id: "$sales.salesByCategory.categoryName", |
| 51 | +... totalSales: { $sum: "$sales.salesByCategory.totalSales" } |
| 52 | +... } |
| 53 | +... } |
| 54 | +... ]) |
| 55 | + |
| 56 | +[ |
| 57 | + { _id: 'Christmas Trees', totalSales: 3147281 }, |
| 58 | + { _id: 'Nuts', totalSales: 3002332 }, |
| 59 | + { _id: 'Camping Tables', totalSales: 4431667 } |
| 60 | +] |
| 61 | + |
| 62 | +``` |
| 63 | + |
| 64 | +### Example 2: Find stores with full-time staff greater than 10 |
| 65 | + |
| 66 | +This example shows how to filter stores where the number of full-time staff is greater than 10. |
| 67 | + |
| 68 | +```javascript |
| 69 | +db.stores.aggregate([ |
| 70 | + { |
| 71 | + $match: { |
| 72 | + "staff.totalStaff.fullTime": { $gt: 10 } |
| 73 | + } |
| 74 | + } |
| 75 | +]) |
| 76 | +``` |
| 77 | + |
| 78 | +#### Sample output |
| 79 | + |
| 80 | +```javascript |
| 81 | +[mongos] StoreData> db.stores.aggregate([ |
| 82 | +... { |
| 83 | +... $match: { |
| 84 | +... "staff.totalStaff.fullTime": { $gt: 10 } |
| 85 | +... } |
| 86 | +... } |
| 87 | +... ]) |
| 88 | + |
| 89 | +[ |
| 90 | + { |
| 91 | + _id: '7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5', |
| 92 | + name: "Lenore's DJ Equipment Store", |
| 93 | + location: { lat: -9.9399, lon: -0.334 }, |
| 94 | + staff: { totalStaff: { fullTime: 18, partTime: 7 } }, |
| 95 | + sales: { |
| 96 | + totalSales: 35911, |
| 97 | + salesByCategory: [ { categoryName: 'DJ Headphones', totalSales: 35911 } ] |
| 98 | + }, |
| 99 | + promotionEvents: [ |
| 100 | + { |
| 101 | + discounts: [ |
| 102 | + { categoryName: 'DJ Turntables', discountPercentage: 18 }, |
| 103 | + { categoryName: 'DJ Mixers', discountPercentage: 15 } |
| 104 | + ] |
| 105 | + } |
| 106 | + ], |
| 107 | + tag: [ '#SeasonalSale', '#FreeShipping', '#MembershipDeals' ] |
| 108 | + } |
| 109 | +] |
| 110 | +``` |
| 111 | + |
| 112 | +### Example 3: List all promotion events with discounts greater than 15% |
| 113 | + |
| 114 | +This example lists all promotion events where any discount is greater than 15%. |
| 115 | + |
| 116 | +```javascript |
| 117 | +db.stores.aggregate([ |
| 118 | + { |
| 119 | + $unwind: "$promotionEvents" |
| 120 | + }, |
| 121 | + { |
| 122 | + $unwind: "$promotionEvents.discounts" |
| 123 | + }, |
| 124 | + { |
| 125 | + $match: { |
| 126 | + "promotionEvents.discounts.discountPercentage": { $gt: 15 } |
| 127 | + } |
| 128 | + }, |
| 129 | + { |
| 130 | + $group: { |
| 131 | + _id: "$promotionEvents.eventName", |
| 132 | + discounts: { $push: "$promotionEvents.discounts" } |
| 133 | + } |
| 134 | + } |
| 135 | +]) |
| 136 | +``` |
| 137 | + |
| 138 | +#### Sample output |
| 139 | + |
| 140 | +```javascript |
| 141 | +[mongos] StoreData> db.stores.aggregate([ |
| 142 | +... { |
| 143 | +... $unwind: "$promotionEvents" |
| 144 | +... }, |
| 145 | +... { |
| 146 | +... $unwind: "$promotionEvents.discounts" |
| 147 | +... }, |
| 148 | +... { |
| 149 | +... $match: { |
| 150 | +... "promotionEvents.discounts.discountPercentage": { $gt: 20 } |
| 151 | +... } |
| 152 | +... }, |
| 153 | +... { |
| 154 | +... $group: { |
| 155 | +... _id: "$promotionEvents.eventName", |
| 156 | +... discounts: { $push: "$promotionEvents.discounts" } |
| 157 | +... } |
| 158 | +... } |
| 159 | +... ]) |
| 160 | +[ |
| 161 | + { |
| 162 | + [ |
| 163 | + { categoryName: 'Basketball Gear', discountPercentage: 23 }, |
| 164 | + { categoryName: 'Wool Carpets', discountPercentage: 22 }, |
| 165 | + { |
| 166 | + categoryName: 'Portable Bluetooth Speakers', |
| 167 | + discountPercentage: 24 |
| 168 | + } |
| 169 | + ] |
| 170 | + } |
| 171 | +] |
| 172 | +``` |
0 commit comments