Time to Code!
Spring Support for MongoDB Aggregations
MongoDB Aggregation Operations
Switch to session2 for this exercise:
git stash
git checkout session2
Question 1: Total Sales by Location
Calculate the total sales grouped by each location and return a result that includes the location's name (or ID) and the total sales amount.
Steps to Implement:
- GroupOperation: Group by storeLocation and count the total sales.
- Aggregation: Combine the group operation into an aggregation.
- Execution: Execute the aggregation and return the results.
Test with:
mvn spring-boot:run -Dspring-boot.run.arguments=total-sales-by-location
Question 2: Customer Satisfaction by Location
Calculate the average customer satisfaction rating for each store location, based on a 'customer.satisfaction' field in each sale document. Only accept satisfaction ratings that are greater than or equal to 1.
Steps to Implement:
- MatchOperation: Filter documents with customer.satisfaction greater than or equal to 1.
- GroupOperation: Group by storeLocation and calculate the average satisfaction.
- ProjectionOperation: Project the results to format the output.
- Aggregation: Combine the operations into an aggregation.
- Execution: Execute the aggregation and return the results.
Test with:
mvn spring-boot:run -Dspring-boot.run.arguments=average-customer-satisfaction
Question 3: Average Price of Items per Store
Calculate the average price of items sold per store and return a result that includes the store's name (or ID) and the average price.
Steps to Implement:
- UnwindOperation: Unwind the items array to process each item separately.
- GroupOperation: Group by storeLocation and calculate the average price of items.
- Aggregation: Combine the operations into an aggregation.
- Execution: Execute the aggregation and return the results.
Test with:
mvn spring-boot:run -Dspring-boot.run.arguments=average-item-price-per-store
Question 4: Number of Distinct Customers
Count the number of distinct customers and return a result that includes the identifier for grouping and the count of customers.
Steps to Implement:
- GroupOperation: Group by storeLocation and collect unique customers using addToSet.
- ProjectionOperation: Project the size of the unique customers set.
- Aggregation: Combine the operations into an aggregation.
- Execution: Execute the aggregation and return the results.
Test with:
mvn spring-boot:run -Dspring-boot.run.arguments=count-distinct-customers
Question 5: Total Sales by Day of the Week
Calculate the total sales grouped by each day of the week and return a result that includes the day's name (or ID) and the total sales amount.
Steps to Implement:
- ProjectionOperation: Project the day of the week from the saleDate.
- GroupOperation: Group by dayOfWeek and count the total sales.
- Aggregation: Combine the operations into an aggregation.
- Execution: Execute the aggregation and return the results.
Test with:
mvn spring-boot:run -Dspring-boot.run.arguments=total-sales-by-day-of-week
Question 6: Sum of Sales Revenue by Location
Calculate the total revenue from sales grouped by each location and return a result that includes the location's name (or ID) and the total revenue. Sort these from highest to lowest.
Steps to Implement:
- UnwindOperation: Unwind the items array to process each item separately.
- ProjectionOperation: Calculate the total revenue for each item.
- GroupOperation: Group by storeLocation and sum the total revenue.
- ProjectionOperation: Project the final results.
- SortOperation: Sort the results by total revenue in descending order.
- Aggregation: Combine the operations into an aggregation.
- Execution: Execute the aggregation and return the results.
Test with:
mvn spring-boot:run -Dspring-boot.run.arguments=revenue-by-location
Question 7: Sales Performance Before and After Applying Coupons
Compare the number of sales transactions with and without coupons at each store location and return a detailed breakdown.
Steps to Implement:
- GroupOperation: Group by storeLocation and count the sales with and without coupons using conditional operators.
- ProjectionOperation: Project the results to format the output.
- Aggregation: Combine the operations into an aggregation.
- Execution: Execute the aggregation and return the results.
Test with:
mvn spring-boot:run -Dspring-boot.run.arguments=sales-performance