MongoTemplate 使用aggregate聚合查询
db.getCollection('trade').aggregate( [
// $match 在$group 前过滤分组前数据 $match在$group之后过滤分组后的文档 { $match : { CheckStatus : "邮件复核不一致" } }, { $group : { _id : "$CheckStatus", tradePriceSum : {$sum : "$TradePrice"}, undlRoundingSum : {$sum : "$UndlRounding"} } }, ] )
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); } }