db.getCollection('trade').aggregate( [
// $match 在$group 前過濾分組前數據 $match在$group以後過濾分組後的文檔 { $match : { CheckStatus : "郵件複覈不一致" } }, { $group : { _id : "$CheckStatus", tradePriceSum : {$sum : "$TradePrice"}, undlRoundingSum : {$sum : "$UndlRounding"} } }, ] )
java java
package com.htsc.thfx.framework.controller; import com.htsc.thfx.framework.vo.ResultMessage; import com.mongodb.BasicDBObject; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.AggregationOperation; import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.aggregation.GroupOperation; import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @RestController @RequestMapping("/mongo") public class MongoController extends BaseController { @Autowired MongoTemplate mongoTemplate; @GetMapping("/find") public ResultMessage find(String[] keys, String[] values, String[] queryKey, @RequestParam(defaultValue = "trade") String collectionName) { // 篩選條件 BasicDBObject dbObject = new BasicDBObject(); for (int i = 0; i < keys.length; i++) { dbObject.put(keys[i], values[i]); } //指定返回的字段 BasicDBObject fieldsObject = new BasicDBObject(); for (String s : queryKey) { fieldsObject.put(s, true); } Query query = new BasicQuery(dbObject.toJson(), fieldsObject.toJson()); List<HashMap> result = mongoTemplate.find(query, HashMap.class, collectionName); return success(result); } @GetMapping("/group") public ResultMessage group(String[] keys, String[] values,@RequestParam String groupKey, String[] queryKey, @RequestParam(defaultValue = "trade") String collectionName) { // 聚合操做 List<AggregationOperation> operations = new ArrayList<>(); // 篩選條件 for (int i = 0; i < keys.length; i++) { //operations.add(Aggregation.match(new Criteria(keys[i]).is(values[i]))); operations.add(Aggregation.match(Criteria.where(keys[i]).is(values[i]))); } // 分組字段 GroupOperation groupOperation = Aggregation.group(groupKey); // 聚合查詢字段 for (int i = 0; i < queryKey.length; i++) { groupOperation = groupOperation.sum(queryKey[i]).as(queryKey[i]); } // 添加選項 (聚合查詢字段和添加篩選是有區別的注意) operations.add(groupOperation); // 最終聚合查詢全部信息 Aggregation aggregation = Aggregation.newAggregation(operations); // 查詢結果 AggregationResults<HashMap> results = mongoTemplate.aggregate(aggregation, collectionName, HashMap.class); //獲取結果 List<HashMap> result = results.getMappedResults(); return success(result); } }