美文网首页
JAVA使用MongoTemplate聚合函数分组并排序

JAVA使用MongoTemplate聚合函数分组并排序

作者: 烟雨酱南 | 来源:发表于2021-09-01 14:29 被阅读0次
        public List<Map<String, Object>> calculationGroupOfText(String code){
            Aggregation agg = Aggregation.newAggregation(Aggregation.match(Criteria.where("key").is("value").and(code).ne(null)),
                    Aggregation.group(code)
                    .first(code).as("name")
                    .count()
                    .as("num"),
                    Aggregation.sort(Sort.Direction.DESC,"num"),
                    Aggregation.limit(5));
            AggregationResults<Map> res = template.aggregate(agg, "文档名", Map.class);
            List<Map> resList = res.getMappedResults();
            List<Map<String, Object>> list = new ArrayList<>();
            for (Map map: resList) {
                if (ValidationUtil.isNotEmpty(map.get("name"))){
                    Map<String, Object> resMap = new HashMap<>(5);
                    resMap.put("number",map.get("num"));
                    resMap.put("name",map.get("name"));
                    list.add(resMap);
                }
            }
            return list;
        }
    

    相关文章

      网友评论

          本文标题:JAVA使用MongoTemplate聚合函数分组并排序

          本文链接:https://www.haomeiwen.com/subject/kbbxwltx.html