monogodb使用$lookup,$unwind,$project,$group,$push进行多表关联查询,提取指定字段,根据指定字段分组

mongo aggregate操作使用$lookup,$unwind,$project,$group,$push操作符,执行多表连接查询,提取多表指定字段,对指定字段进行分组求和得到结果:

db.getCollection('DeviceDetailInfo_20191230').aggregate([
{$lookup:{from:'DeviceDetailInfo_20191229',localField:"deviceId",foreignField:"deviceId",as:"basicInfo"}},
{$unwind:{path: "$basicInfo",preserveNullAndEmptyArrays:false}},
{$project:{_id:false,"bareacode":"$basicInfo.areadcode","bmanufacturer":"$basicInfo.manufacturer","bcountycode":"$basicInfo.countycode",periodic_count:"$periodic_count",subDeviceNumber:"$subDeviceNumber"}},
{$group:{_id:{areacode:"bareacode",manufacturer:"$bmanufacturer",countycode:"$bcountycode"},count1:{"$sum":"$periodic_count"},count2:{"$sum":"$subDeviceNumber"}}}
]);

语句指令说明:

[
    {
        "$lookup":{   //关联查询指令
            "from":"DeviceDetailInfo_20191229",  //连接目标表
            "localField":"deviceId",  //本表属性
            "foreignField":"deviceId",   //目标表属性
            "as":"basicInfo"   //关联进来的内容放在basicInfo数组中,因为存在一对多的映射所以为数组
        }
    },
    {
        "$unwind":{
            "path":"$basicInfo",  //根据数组每一项拆分成单个文档
            "preserveNullAndEmptyArrays":"false"  //true:如果匹配到的数组为空输出空文档,fasle:如果匹配到的数组为空不输出文档
        }
    },
    {
        "$project":{   //选择默认属性输出
            "_id":false,    //不自动生成主键
            "bareacode":"$basicInfo.areadcode",   //指定basicInfo.areadcode字段的别名为bareacode
            "bmanufacturer":"$basicInfo.manufacturer",
            "bcountycode":"$basicInfo.countycode",
            "periodic_count":"$periodic_count",
            "subDeviceNumber":"$subDeviceNumber",
            "maxTxRate":"$maxTxRate",
             "averTxRate":"$averTxRate",
             "averRxRate":"$averRxRate",
             "maxRxRate":"$maxRxRate",
             "wlanRadioPower":"$wlanRadioPower"
        }
    },
    {
        "$group":{  //对选择出的属性字段进行分组聚合求和
            "_id":{     //根据如下属性进行分组并指定字段别名
                "areacode":"$bareacode",
                "manufacturer":"$bmanufacturer",
                "countycode":"$bcountycode"
            },
            "subList":{
                    "$push":{   //对于组内数据。指定属性构建数组结构
                        "maxTxRate":"$maxTxRate",
                        "averTxRate":"$averTxRate",
                        "averRxRate":"$averRxRate",
                        "maxRxRate":"$maxRxRate",
                        "wlanRadioPower":"$wlanRadioPower"
                    }
                },
            "count1":{      //指定求和结果的别名
                "$sum":"$periodic_count"
            },
            "count2":{
                "$sum":"$subDeviceNumber"
            }
        }
    }
]

使用spring boot 2.x进行mongodb聚合查询使用match,group,unwind,lookup,project算子。
代码示例:先match在group出结果再lookup,unwind到所需字段最后project得到目标结果级,以游标cursor方式输出。

        Criteria criteria = new Criteria();
        MatchOperation match = Aggregation.match(criteria);
        GroupOperation group = Aggregation.group("deviceId").count().as("sum").last("PONRXPOWER")
                .as("ponRxPower").last("ONUTXPOWER").as("onuTxPower").last("actualTime")
                .as("actualTime");
        LookupOperation lookup = Aggregation.lookup("LatestDeviceInfo", "_id", "_id", "basicInfo");
        UnwindOperation unwind = Aggregation.unwind("basicInfo", "arrayIndex", true);
        Fields fields = Fields.fields("_id", "sum", "basicInfo.areacode", "basicInfo.manufacturer",
                "basicInfo.model", "basicInfo.PPPOEUser", "basicInfo.countycode",
                "basicInfo.RoleId", "basicInfo.OnuName", "basicInfo.BrasIP", "basicInfo.OltIP",
                "basicInfo.PonIP", "basicInfo.mac", "basicInfo.hardwareVersion",
                "basicInfo.firmwareVersion", "basicInfo.dpiVersion", "basicInfo.CommunityInfo",
                "basicInfo.emsName", "basicInfo.oltName", "basicInfo.userName201",
                "basicInfo.ponPort", "basicInfo.installaddr", "ponRxPower", "onuTxPower",
                "actualTime");
        ProjectionOperation project = Aggregation.project(fields);

        Aggregation aggregation = TypedAggregation.newAggregation(match, group, lookup, unwind, project);
        List<Document> pipeline = Document.parse(aggregation.toString()) .getList("pipeline", Document.class);
        MongoCursor<Document> cursor = mongoTemplate.getCollection(""collectionName").
                            aggregate(pipeline , Document.class).allowDiskUse(true)
                    .batchSize(1000).maxTime(5, TimeUnit.MINUTES).cursor();
       while (cursor.hasNext()) {
                   //具体业务逻辑
     }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章