上一篇筆記僅是記錄了一下簡單的關聯查詢,根據筆記中的場景:將某一車輛關聯的耗損記錄所有放在了一個字段當中。不知道如今中有沒有這種場景,咱們的應用中沒有相似的場景,可能咱們更關注的是某車輛的總耗損金額和總營收金額、總里程數等,所以這篇筆記記錄一下怎麼獲得這些數據。要想獲取這些記錄咱們首先想到的就是先按照車輛ID分組以後再對指定字段求和,多是先入爲主的緣由(有了上一篇筆記),我沒有按照傳統的思路走下去,而是想着看看能不能在上一篇筆記的基礎之上獲得想要的結果。mongodb
咱們沿着這條路在分析一下,車輛相關的耗損和營收均可以放到一個字段中。也就是說咱們不用分組了,車輛相關的耗損和營收記錄都冗餘到了車輛信息記錄中,那麼接下來就是對數組中的某一字段或者多個進行求和了……查了半天並無找到相似$sum這樣的聚合管道能夠對數組中的字段求和,不過得益於mongodb管道的強大,用另一個管道間接實現了這個功能。數組
db.getCollection('FormInstace').aggregate([ { $match: { "_id": { $in: ["1","2","3"] }, "FormItems.key": { $ne: null } } }, { $addFields: { FormValueObj: { $arrayToObject: { $map: { input: "$FormItems", as: "field", in: [ "$$field.key", "$$field.value" ] } } } } }, { $replaceRoot: { newRoot: { $mergeObjects: [ "$FormValueObj", "$$ROOT" ] } } }, { $project: { FormItems:0, FormValueObj:0 } }, { $lookup:{ from:"FormInstace", let: { tempMainRelationKey: "$_id" }, pipeline:[ { $match: { FormId: "507048044944692000", "FormItems.key": { $ne: null } } }, { $addFields: { FormValueObj: { $arrayToObject: { $map: { input: "$FormItems", as: "field", in: [ "$$field.key", "$$field.value" ] } } } } }, { $replaceRoot: { newRoot: { $mergeObjects: [ "$FormValueObj", "$$ROOT" ] } } }, { $project: { FormItems:0, FormValueObj:0 } }, { $match: { $expr: { $eq: [ "$1572493552001.id","$$tempMainRelationKey" ] } } }, ], as:'carWastage' } }, { $lookup:{ from:"FormInstace", let: { tempMainRelationKey: "$_id" }, pipeline:[ { $match: { FormId: "507048044944693000", "FormItems.key": { $ne: null } } }, { $addFields: { FormValueObj: { $arrayToObject: { $map: { input: "$FormItems", as: "field", in: [ "$$field.key", "$$field.value" ] } } } } }, { $replaceRoot: { newRoot: { $mergeObjects: [ "$FormValueObj", "$$ROOT" ] } } }, { $project: { FormItems:0, FormValueObj:0 } }, { $match: { $expr: { $eq: [ "$1572493553001.id","$$tempMainRelationKey" ] } } }, ], as:'carRevenue' } }, { $addFields: { carWastageStatistics: { $reduce: { input: "$carWastage", initialValue: {wastageMoney:0}, in: { wastageMoney:{ $add : ["$$value.wastageMoney", "$$this.1572493552005"] } } } } } }, { $addFields: { carRevenueStatistics: { $reduce: { input: "$carRevenue", initialValue: {revenueMoney:0,mileage:0}, in: { revenueMoney:{$add : ["$$value.revenueMoney", "$$this.1572493553005"]}, mileage:{$add : ["$$value.mileage", "$$this.1572493553006"]} } } } } } ]);
此次統計查詢就查出了某些車輛的總耗損金額和總營收金額、總里程數;只不過將結果放到對象中了(方便統計一個表中的多個字段),可是如今看不到具體的結果,將代碼在修改一下,看一下結果ide
db.getCollection('FormInstace').aggregate([ { $match: { "_id": { $in: ["1","2","3"] }, "FormItems.key": { $ne: null } } }, { $addFields: { FormValueObj: { $arrayToObject: { $map: { input: "$FormItems", as: "field", in: [ "$$field.key", "$$field.value" ] } } } } }, { $replaceRoot: { newRoot: { $mergeObjects: [ "$FormValueObj", "$$ROOT" ] } } }, { $project: { FormItems:0, FormValueObj:0 } }, { $lookup:{ from:"FormInstace", let: { tempMainRelationKey: "$_id" }, pipeline:[ { $match: { FormId: "507048044944692000", "FormItems.key": { $ne: null } } }, { $addFields: { FormValueObj: { $arrayToObject: { $map: { input: "$FormItems", as: "field", in: [ "$$field.key", "$$field.value" ] } } } } }, { $replaceRoot: { newRoot: { $mergeObjects: [ "$FormValueObj", "$$ROOT" ] } } }, { $project: { FormItems:0, FormValueObj:0 } }, { $match: { $expr: { $eq: [ "$1572493552001.id","$$tempMainRelationKey" ] } } }, ], as:'carWastage' } }, { $lookup:{ from:"FormInstace", let: { tempMainRelationKey: "$_id" }, pipeline:[ { $match: { FormId: "507048044944693000", "FormItems.key": { $ne: null } } }, { $addFields: { FormValueObj: { $arrayToObject: { $map: { input: "$FormItems", as: "field", in: [ "$$field.key", "$$field.value" ] } } } } }, { $replaceRoot: { newRoot: { $mergeObjects: [ "$FormValueObj", "$$ROOT" ] } } }, { $project: { FormItems:0, FormValueObj:0 } }, { $match: { $expr: { $eq: [ "$1572493553001.id","$$tempMainRelationKey" ] } } }, ], as:'carRevenue' } }, { $addFields: { carWastage_Money: { $reduce: { input: "$carWastage", initialValue: 0, in: { $add : ["$$value", "$$this.1572493552005"] } } } } }, { $addFields: { carRevenue_Money: { $reduce: { input: "$carRevenue", initialValue: 0, in: { $add : ["$$value", "$$this.1572493553005"] } } } } }, { $addFields: { carRevenue_Mileage: { $reduce: { input: "$carRevenue", initialValue: 0, in: { $add : ["$$value", "$$this.1572493553006"] } } } } } ]);
這樣就一目瞭然了。統計結果對不對呢?咱們用傳統的分組以後再求和在查詢一下,將結果作一下對比(順便記錄一下,分組求和)this
db.getCollection('FormInstace').aggregate([ { $match: { "FormId":"507048044944692000", "FormItems.key": { $ne: null } } }, { $addFields: { FormValueObj: { $arrayToObject: { $map: { input: "$FormItems", as: "field", in: [ "$$field.key", "$$field.value" ] } } } } }, { $replaceRoot: { newRoot: { $mergeObjects: [ "$FormValueObj", "$$ROOT" ] } } }, { $match: { "1572493552001.id": { $in:["1","2","3"]} } }, { $project: { FormItems:0, FormValueObj:0 } }, { $group:{ _id: "$1572493552001.id", wastageMoney: { $sum: "$1572493552005"} } }, { $sort : { _id: 1 } } ]);
db.getCollection('FormInstace').aggregate([ { $match: { "FormId":"507048044944693000", "FormItems.key": { $ne: null } } }, { $addFields: { FormValueObj: { $arrayToObject: { $map: { input: "$FormItems", as: "field", in: [ "$$field.key", "$$field.value" ] } } } } }, { $replaceRoot: { newRoot: { $mergeObjects: [ "$FormValueObj", "$$ROOT" ] } } }, { $match: { "1572493553001.id": { $in:["1","2","3"]} } }, { $project: { FormItems:0, FormValueObj:0 } }, { $group:{ _id: "$1572493553001.id", wastageMoney: { $sum: "$1572493553005"}, mileage: { $sum: "$1572493553006"} } }, { $sort : { _id: 1 } } ]);
對比兩次查詢結果,兩次不一樣的統計方式的結果相同。這樣就放心了。好了就到這裏了。spa