栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot将MongoDB聚合语句转Java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot将MongoDB聚合语句转Java

文章目录
原MongoDB语句 :

db.batch_operation_log.aggregate([
    {
        $project: {
            "queryIndex": 1,
            "status": 1,
            AType: {
                $cond: {
                    if : {
                        $eq: ["IN_PROGRESS", "$status"]
                    },
                    then: 1,
                    else : 0
                }
            },
            BType: {
                $cond: {
                    if : {
                        $eq: ["FAIL", "$status"]
                    },
                    then: 1,
                    else : 0
                }
            }
        }
    },
    {
        $group: {
            _id: {
                "status": "$status",
                "queryIndex": "$queryIndex"
            },
            queryIndex: {
                "$first": "$queryIndex"
            },
            IN_PROGRESS: {
                $sum: "$AType"
            },
            FAIL: {
                $sum: "$BType"
            }
        }
    },
    {
        $group: {
            _id: {
                "queryIndex": "$queryIndex"
            },
            queryIndex: {
                "$first": "$queryIndex"
            },
            inprogress: {
                $sum: "$IN_PROGRESS"
            },
            fail: {
                $sum: "$FAIL"
            }
        }
    }
])

SpringBoot API 转换 :

//通过任务id和阶段id进行分组 ,获取成功数和失败数
        List operations = new ArrayList<>();
        //匹配
        //operations.add(Aggregation.match(Criteria.
               //where("queryIndex").in(queryIndex)));
        //列投影
        operations.add(Aggregation.project("queryIndex", "status")
                .andexpression("{$cond: { if : { $eq: {'IN_PROGRESS', '$status'}},then:1,else:0}}").as("AType")
                .andexpression("{$cond: { if : { $eq: {'FAIL', '$status'}},then:1,else:0}}").as("BType"));
        //分组
        operations.add(Aggregation.group("status", "queryIndex")
                .sum("AType").as("IN_PROGRESS")
                .sum("BType").as("FAIL")
                .first("queryIndex").as("queryIndex"));
        //继续分组
        operations.add(Aggregation.group("queryIndex")
                .sum("IN_PROGRESS").as("inProgress")
                .sum("FAIL").as("fail")
                .first("queryIndex").as("queryIndex"));
        AggregationResults results = mongoTemplate.aggregate(Aggregation.newAggregation(
                operations.toArray(new AggregationOperation[0])
        ), "batch_operation_log", QueryCountDO.class);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/684757.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号