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()) {
//具體業務邏輯
}