MongoTemplate 使用aggregate聚合查询
Posted ronniery
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); } }
以上是关于MongoTemplate 使用aggregate聚合查询的主要内容,如果未能解决你的问题,请参考以下文章
带有 MongoTemplate 的 Spring Boot
使用 Spring Boot 和 MongoTemplate 对 MongoDB 结果进行排序
MongoDB $addFields 使用 org.springframework.data.mongodb.core.MongoTemplate