SpringBoot+mongodb实现分组统计、时间范围查询、去重排序查询、组合排序、分页查询

SpringBoot+mongodb实现分组统计、时间范围查询、去重排序查询、组合排序

一、分组统计(Aggregation)

1.按某字段分组统计

    /**
     * 根据分类名称 分组统计销售数量
     * @param dataType 1:本日 2:本月 3:本年
     * @return
     */
    @RequestMapping("/findInfoByGroup")
    public List<Map> findInfoByGroup(){
   
        //从mongodb里获取满足条件的记录   status:状态 1:上架  2:下架
        Criteria criteria = Criteria.where("status").is(1);
        //聚合函数查询统计信息
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                //按分类名称 统计 销售数量
                Aggregation.group("category").sum("sale_num").as("total"),
                //按照total降序
                Aggregation.sort(Sort.Direction.DESC,"total")
        );
        AggregationResults<Map> aggregationResults = mongoTemplate.aggregate(aggregation,Product.class,Map.class);
        List<Map> list = aggregationResults.getMappedResults();

        return list;
    }

category:要分组的字段
sale_num:要统计的字段
total:别名

返回结果:

[
    {
   
        "_id": "口红",
        "total": 2224.0
    },
    {
   
        "_id": "面膜",
        "total": 997.0
    },
    {
   
        "_id": "文具",
        "total": 799.0
    },
    {
   
        "_id": "男装",
        "total": 675.0
    },
    {
   
        "_id": "女装",
        "total": 448.0
    }
]

_id本来应该是对应查询的字段,没有处理,spring会将分组字段自动转换为_id

2.多字段分组统计

    /**
     * 根据分类名称和规格 分组统计销售数量
     * @param
     * @return
     */
    @RequestMapping("/findInfoByGroup1")
    public List<Map> findInfoByGroup1(){
   
        //从mongodb里获取最新的一条记录   status:状态 1:上架  2:下架
        Criteria criteria = Criteria.where("status").is(1);
        //聚合函数查询统计信息
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                //按分类名称 统计 销售数量
                Aggregation.group("category","specification").sum("sale_num").as("total"),
                //按照total降序
                Aggregation.sort(Sort.Direction.DESC,"total")
        );
        AggregationResults<Map> aggregationResults = mongoTemplate.aggregate(aggregation,Product.class,Map.class);
        List<Map> list = aggregationResults.getMappedResults();

        return list;
    }

返回结果:

[
    {
   
        "_id": {
   
            "category": "口红",
            "specification": "个"
        },
        "total": 2224.0
    },
    {
   
        "_id": {
   
            "category": "面膜",
            "specification": "盒"
        },
        "total": 997.0
    },
    {
   
        "_id": {
   
            "category": "文具",
            "specification": "套"
        },
        "total": 799.0
    },
    {
   
        "_id": {
   
            "category": "男装",
            "specification": "件"
        },
        "total": 675.0
    },
    {
   
        "_id": {
   
            "category": "女装",
            "specification": "件"
        },
        "total": 448.0
    }
]

二、时间范围查询

方式一:


                
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值