我想在实习后达到这个结果:
dataset: [
{
seriesname: "GROUP 1",
data: [
{
stage: 'START'
value: 3
},
{
stage: 'END'
value: 4
}
]
},
{
seriesname: "GROUP 2",
data: [
{
stage: 'START'
value: 3
},
{
stage: 'END'
value: 1
}
]
}
]
{ $unwind: '$name' },
{ $group: {
_id: '$name.name',
data: { $push: {
$first: {
'value': { $sum: {
$cond: [
{ $and: [
{ $eq: ['$stage', 'START'] },
{ $gte: ['$dStart', new Date(dStart)] },
{ $lte: ['$dEnd', new Date(dEnd)] }
] }, parseInt(1), parseInt(0)] } },
'stage': 'START'
},
$first: {
'value': { $sum: {
$cond: [
{ $and: [
{ $eq: ['$two', 'END'] },
{ $gte: ['$dStart', new Date(dStart)] },
{ $lte: ['$dEnd', new Date(dEnd)] }
] }, parseInt(1), parseInt(0)] } },
'stage': 'END'
}
}}
} }
我如何正确处理所有这些阶段?
答案 0 :(得分:1)
将其分为两个阶段,首先进行分组并计算每个值,然后按需要重组数据,如下所示:
{
$group: {
_id: "$name.name",
start: {
$sum: {
$cond: [
{ $and: [
{ $eq: ['$stage', 'START'] },
{ $gte: ['$dStart', date_param_1] },
{ $lte: ['$dEnd', date_param_2] }
] }, 1, 0]
}
},
end: {
$sum: {
$cond: [
{ $and: [
{ $eq: ['$two', 'END'] },
{ $gte: ['$dStart', date_param_1] },
{ $lte: ['$dEnd', date_param_2] }
] }, 1, 0]
}
}
}
},
{
$addFields: {
data: [{value: "$start", stage: "START}, {value: "$end, stage: "END"}]
}
}