首先是将两个数组映射为一个数组。
其次是过滤单个数组,使其只包含HIGH(/LOW),并将过滤后的数组减少为单个sum元素。
[{$project: {
"Quality": {
$map: {
input: {$range: [0, {$size: "$Quality"}]},
as: "idx",
in: {
"Quality": { $arrayElemAt: ["$Quality", "$$idx"] },
"Pages": { $arrayElemAt: ["$Pages", "$$idx"] }
}
}
}
}}, {$project: {
"HIGH": {
$reduce: {
input: {
$filter: {
input: "$Quality",
as: "x",
cond: {$eq: ["$$x.Quality", "HIGH"]}
}
},
initialValue: 0,
in: {
$add: ["$$value", "$$this.Pages"]
}
}
},
"LOW": {
$reduce: {
input: {
$filter: {
input: "$Quality",
as: "x",
cond: {$eq: ["$$x.Quality", "LOW"]}
}
},
initialValue: 0,
in: {
$add: ["$$value", "$$this.Pages"]
}
}
}
}}]
这正是你所要求的:
{
_id: 1
HIGH:27
LOW:22
}