diff --git a/reference/commands/aggregation/aggregate.md b/reference/commands/aggregation/aggregate.md new file mode 100644 index 0000000..ddf53b6 --- /dev/null +++ b/reference/commands/aggregation/aggregate.md @@ -0,0 +1,172 @@ +--- +title: Aggregate +description: The aggregate command is used to process data records and return computed results. +type: commands +category: aggregation +--- + +# aggregate + +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. + +## Syntax + +```console +db.collection.aggregate(pipeline, options) +``` + +- **pipeline**: An array of aggregation stages that process and transform the data. +- **options**: Optional. Specifies more options for the aggregation, such as `explain`, `allowDiskUse`, and `cursor`. + +## Examples + +### Example 1: Calculate total sales by category + +This example demonstrates how to calculate the total sales for each category in the `stores` collection. + +```javascript +db.stores.aggregate([ + { + $unwind: "$sales.salesByCategory" + }, + { + $group: { + _id: "$sales.salesByCategory.categoryName", + totalSales: { $sum: "$sales.salesByCategory.totalSales" } + } + } +]) +``` + +#### Sample output + +```javascript +[mongos] StoreData> db.stores.aggregate([ +... { +... $unwind: "$sales.salesByCategory" +... }, +... { +... $group: { +... _id: "$sales.salesByCategory.categoryName", +... totalSales: { $sum: "$sales.salesByCategory.totalSales" } +... } +... } +... ]) + +[ + { _id: 'Christmas Trees', totalSales: 3147281 }, + { _id: 'Nuts', totalSales: 3002332 }, + { _id: 'Camping Tables', totalSales: 4431667 } +] + +``` + +### Example 2: Find stores with full-time staff greater than 10 + +This example shows how to filter stores where the number of full-time staff is greater than 10. + +```javascript +db.stores.aggregate([ + { + $match: { + "staff.totalStaff.fullTime": { $gt: 10 } + } + } +]) +``` + +#### Sample output + +```javascript +[mongos] StoreData> db.stores.aggregate([ +... { +... $match: { +... "staff.totalStaff.fullTime": { $gt: 10 } +... } +... } +... ]) + +[ + { + _id: '7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5', + name: "Lenore's DJ Equipment Store", + location: { lat: -9.9399, lon: -0.334 }, + staff: { totalStaff: { fullTime: 18, partTime: 7 } }, + sales: { + totalSales: 35911, + salesByCategory: [ { categoryName: 'DJ Headphones', totalSales: 35911 } ] + }, + promotionEvents: [ + { + discounts: [ + { categoryName: 'DJ Turntables', discountPercentage: 18 }, + { categoryName: 'DJ Mixers', discountPercentage: 15 } + ] + } + ], + tag: [ '#SeasonalSale', '#FreeShipping', '#MembershipDeals' ] + } +] +``` + +### Example 3: List all promotion events with discounts greater than 15% + +This example lists all promotion events where any discount is greater than 15%. + +```javascript +db.stores.aggregate([ + { + $unwind: "$promotionEvents" + }, + { + $unwind: "$promotionEvents.discounts" + }, + { + $match: { + "promotionEvents.discounts.discountPercentage": { $gt: 15 } + } + }, + { + $group: { + _id: "$promotionEvents.eventName", + discounts: { $push: "$promotionEvents.discounts" } + } + } +]) +``` + +#### Sample output + +```javascript +[mongos] StoreData> db.stores.aggregate([ +... { +... $unwind: "$promotionEvents" +... }, +... { +... $unwind: "$promotionEvents.discounts" +... }, +... { +... $match: { +... "promotionEvents.discounts.discountPercentage": { $gt: 20 } +... } +... }, +... { +... $group: { +... _id: "$promotionEvents.eventName", +... discounts: { $push: "$promotionEvents.discounts" } +... } +... } +... ]) +[ + { + [ + { categoryName: 'Basketball Gear', discountPercentage: 23 }, + { categoryName: 'Wool Carpets', discountPercentage: 22 }, + { + categoryName: 'Portable Bluetooth Speakers', + discountPercentage: 24 + } + ] + } +] +``` diff --git a/reference/commands/aggregation/distinct.md b/reference/commands/aggregation/distinct.md new file mode 100644 index 0000000..3416825 --- /dev/null +++ b/reference/commands/aggregation/distinct.md @@ -0,0 +1,101 @@ +--- +title: Distinct +description: The distinct command is used to find the unique values for a specified field across a single collection. +type: commands +category: aggregation +--- + +# distinct + +The `distinct` command is used to find the unique values for a specified field across a single collection. This command is useful when you need to identify the set of distinct values for a field without retrieving all the documents or when you need to perform operations like filtering or grouping based on unique values. + +## Syntax + +The basic syntax of the `distinct` command is as follows: + +```javascript +db.collection.distinct(field, query, options) +``` + +- `field`: The field that receives the returned distinct values. +- `query`: Optional. A query that specifies the documents from which to retrieve the distinct values. +- `options`: Optional. Other options for the command. + +## Examples + +Here are examples using the provided sample JSON structure. + +### Example 1: Find distinct categories in sales + +To find the distinct `categoryName` in the `salesByCategory` array: + +```javascript +db.stores.distinct("sales.salesByCategory.categoryName") +``` + +#### Sample output + +```javascript +[mongos] StoreData> db.stores.distinct("sales.salesByCategory.categoryName") +[ + { + _id: 'Discount Derby', + discounts: [ + { categoryName: 'Bath Sheets', discountPercentage: 25 }, + { categoryName: 'Tablecloths', discountPercentage: 25 }, + { categoryName: 'Drapes', discountPercentage: 25 } + ] + } +] +[mongos] StoreData> db.stores.distinct("sales.salesByCategory.categoryName") +[ + 'Music Theory Books', + 'Superfoods', + 'Harmonicas', + 'Garden Tools', + ... 883 more items +] +``` + +### Example 2: Find distinct event names in promotion events + +To find the distinct `eventName` in the `promotionEvents` array: + +```javascript +db.stores.distinct("promotionEvents.eventName") +``` + +#### Sample output + +```javascript +[mongos] StoreData> db.stores.distinct("promotionEvents.eventName") +[ +{ + _id: 'Super Saver Celebration', + discounts: [ + { categoryName: 'Face Towels', discountPercentage: 25 }, + { categoryName: 'Printer Ribbons', discountPercentage: 25 }, + { categoryName: 'Chromebooks', discountPercentage: 25 } + ] + } +] +``` + +### Example 3: Find distinct discount percentages for a specific event + +To find the distinct `discountPercentage` in the `discounts` array for the "Summer Sale" event: + +```javascript +db.stores.distinct("promotionEvents.discounts.discountPercentage", { "promotionEvents.eventName": "Incredible Discount Days" }) +``` + +#### Sample output + +```javascript +[mongos] StoreData> db.stores.distinct("promotionEvents.discounts.discountPercentage", { "promotionEvents.eventName": "Incredible Discount Days" }) +[ + 6, 17, 22, 25, 9, 15, 14, + 7, 12, 19, 24, 5, 20, 10, + 23, 16, 18, 21, 13, 11, 8 +] +```