筆記:使用mongo聚合查詢(一開始根本沒接觸過mongo,一點一點慢慢的查資料完成了工作需求) 需求:在訂單表中,根據buyerNick分組,統計每個buyerNick的電話、地址、支付總金額以及總商品數,返回結果是CustomerDetail。 Trade表: CustomerDetail: O ...
筆記:使用mongo聚合查詢(一開始根本沒接觸過mongo,一點一點慢慢的查資料完成了工作需求)
需求:在訂單表中,根據buyerNick分組,統計每個buyerNick的電話、地址、支付總金額以及總商品數,返回結果是CustomerDetail。
/* * project:列出所有本次查詢的欄位,包括查詢條件的欄位和需要搜索的欄位; * match:搜索條件criteria * unwind:某一個欄位是集合,將該欄位分解成數組 * group:分組的欄位,以及聚合相關查詢 * sum:求和(同sql查詢) * count:數量(同sql查詢) * as:別名(同sql查詢) * addToSet:將符合的欄位值添加到一個集合或數組中 * sort:排序 * skip&limit:分頁查詢 */ public List<CustomerDetail> customerDetailList(Integer pageNum,String userId,String buyerNick,String itemId,List<String> phones) throws Exception{ Criteria criteria = Criteria.where("userId").is(userId); Integer pageSize = 10; Integer startRows = (pageNum - 1) * pageSize; if(buyerNick != null && !"".equals(buyerNick)){ criteria.and("buyerNick").is(buyerNick); } if(phones != null && phones.size() > 0){ criteria.and("mobile").in(phoneList); } if(itemId != null && !"".equals(itemId)){ criteria.and("orders.numIid").is(itemId); } Aggregation customerAgg = Aggregation.newAggregation( Aggregation.project("buyerNick","payment","num","tid","userId","address","mobile","orders"), Aggregation.match(criteria),
Aggregation.unwind("orders"), Aggregation.group("buyerNick").first("buyerNick").as("buyerNick").first("mobile").as("mobile"). first("address").as("address").sum("payment").as("totalPayment").sum("num").as("itemNum").count().as("orderNum"), Aggregation.sort(new Sort(new Sort.Order(Sort.Direction.DESC, "totalPayment"))), Aggregation.skip(startRows), Aggregation.limit(pageSize) ); List<CustomerDetail> customerList = tradeRepository.findAggregateList(new Query(criteria), userId, customerAgg,CustomerDetail.class); return customerList; }
public <T> List<T> findAggregateList(Query query,String userNickName, Aggregation aggregation,Class<T> clazz) { String collectionName = getCollectionName(userNickName); AggregationResults<T> aggregate = this.mongoTemplate.aggregate(aggregation, collectionName, clazz); List<T> customerDetails = aggregate.getMappedResults(); return customerDetails; }
Trade表:
public class TradeInfo{ private String tid;//訂單id private Double payment;//支付金額 private String buyerNick;//買家昵稱 private String address;//地址 private String mobile;//手機號 private Long num;//購買商品數量 private List<order> orders;子訂單 }
CustomerDetail:
public class CustomerDetail{ private String buyerNick;//買家昵稱 private Double totalPayment;//訂單金額 private Integer orderNum;//訂單數 private Integer itemNum;//商品數 private String address;//地址 }
OVER!