springboot 和mongo 聚合多个参数及分页排序和创建索引

我们在使用mongodb有时候需要 group by 参数1,参数2,并在分组中进行排序分页操作。这里我将这种例子展示出来,大家可以作为参考。如果需要查看mongo和springboot结合的配置可以参考,下面这两篇文章。

https://blog..net/fajing_feiyue/article/details/103330764
https://blog..net/fajing_feiyue/article/details/103110616

下面的代码主要展示,mongo在多次goup by的应用。

@Override
    public JsonVO testMongoFind2() {
    	//效果等同于
        //select sum(valueInt) as sumNum,count(1) as countNum,
        count(distinct valueStr2) as distNum,valueStr from table where valueInt>=0 
        group by valueStr order by countNum

        Sort sort = Sort.by(Sort.Direction.ASC, "countNum");

        Criteria criteria = Criteria.where("valueInt").gte(0);

        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                //由于需要distinct valuesStr2 ,故第一次group by valueStr,valuesStr2
                Aggregation.group("valueStr","valueStr2").count().as("countNum").
                        sum("valueInt").as("sumNum"),
                //将上一步获取到数 ,作为下一步使用到的参数
                Aggregation.project("countNum").and("sumNum").as("sumNum").and("valueStr").as("valueStr").
                        and("valueStr2").as("valueStr2"),
                //再次group by valueStr(这个时候就相当于,通过第一次的goupby 对valuesStr2 进行去重操作),最后一次需要将first这样才会有值
              Aggregation.group("valueStr").sum("sumNum").as("sumNum")
                    .sum("countNum").as("countNum").count().as("distNum").first("valueStr").as("valueStr"),
              Aggregation.project("sumNum").and("countNum").as("countNum")
                    .and("distNum").as("distNum").and("valueStr").as("valueStr"),
                Aggregation.sort(sort),
                Aggregation.skip(0L),
                Aggregation.limit(10));

        AggregationResults<AggregationVO2> mongoTest = mongoTemplate.aggregate(aggregation, "mongoTest", AggregationVO2.class);
        List<AggregationVO2> mappedResults = mongoTest.getMappedResults();
        System.out.println(mappedResults);

        JsonVO jsonVO = new JsonVO(200, "success");
        jsonVO.setData(mappedResults);
        return jsonVO;
    }

这里附上展示的内容。

@Data
public class AggregationVO2 {
    private Integer sumNum;
    private Integer countNum;
    private Integer distNum;
    private String valueStr;
    //private String valueStr2;
    
}

这里是返回的结果

{
    "statusCode": 200,
    "errorMessage": "success",
    "data": [
        {
            "sumNum": 5,
            "countNum": 1,
            "distNum": 1,
            "valueStr": ""
        },
        {
            "sumNum": 5,
            "countNum": 1,
            "distNum": 1,
            "valueStr": null
        },
        {
            "sumNum": 16,
            "countNum": 2,
            "distNum": 1,
            "valueStr": "wohao"
        },
        {
            "sumNum": 12,
            "countNum": 2,
            "distNum": 2,
            "valueStr": "nihao"
        }
    ]
}

另外我们在使用mongodb的时候,有时候需要使用代码创建索引

public JsonVO createIndex(){
       //new Document
        org.bson.Document document = new org.bson.Document();
        document.put("valueInt", 1);
        document.put("time", -1);

        Index index = new CompoundIndexDefinition(document);
        index.named("test_index");
        index.background();
        mongoTemplate.indexOps("mongoTest").ensureIndex(index);
        JsonVO jsonVO = new JsonVO(200, "success");


        return jsonVO;
        
    }

这里是查看索引和索引大小的两条命令

db.mongoTest.getIndexes()

db.mongoTest.totalIndexSize()

可以看到索引已经创建成功。

经验分享 程序员 微信小程序 职场和发展