gpt4 book ai didi

javascript - 如何展平聚合输出的 JSON 结果

转载 作者:可可西里 更新时间:2023-11-01 09:47:58 26 4
gpt4 key购买 nike

我使用 mongo db 聚合编写了以下 NodeJs api。我得到了输出,但没有得到我预期的输出,那么该怎么做,有人可以帮助我解决这个问题吗?

app.get('/polute', function (req, res) {
Light.aggregate([
{ $match: {
CREATE_DATE: {
$lte:new Date(),
$gte: new Date(new Date().setDate(new
Date().getDate()-120)
)
}
} },
{ $group: {
_id:{
month: { $month: "$CREATE_DATE" },
year: { $year: "$CREATE_DATE" }
},
avgofozone:{$avg:"$OZONE"}
} },
{ $sort:{ "year": -1 } },
{ $project: {
year: '$_id.year',
avgofozone: '$avgofozone',
month: '$_id.month',_id:0
} }
], function (err, polute) {
console.log("naresh:" +JSON.stringify(polute));
res.json(polute);
});
});

实际输出:

[
{ "avgofozone" : 21.07777777777778, "year" : 2018, "month" : 2 }
{ "avgofozone" : 17.8, "year" : 2018, "month" : 3 }
{ "avgofozone" : 17.8, "year" : 2018, "month" : 1 }
]

预期输出:

[
{
"zone_type": "avgofozone",
"year": 2018,
"February": 21.07777777777778,
"March": 17.8,
"January": 17.8
}
]

最佳答案

使用 MongoDb 3.6 和更新版本,您可以利用 $arrayToObject 运算符和一个 $replaceRoot 管道以获得所需的 JSON 输出。您需要运行以下聚合管道:

const monthsEnum = {
"_id": "year",
"1": "January",
"2": "February",
"3": "March",
"4": "April",
"5": "May",
"6": "June",
"7": "July",
"8": "August",
"9": "September",
"10": "October",
"11": "November",
"12": "December"
};

Light.aggregate([
{ "$match": {
"CREATE_DATE": {
"$lte": new Date(),
"$gte": new Date(new Date().setDate(new Date().getDate()-120))
}
} },
{ "$group": {
"_id": {
"month": { "$month": "$CREATE_DATE" },
"year": { "$year": "$CREATE_DATE" }
},
"avgofozone": { "$avg": "$OZONE" }
} },
{ "$group": {
"_id": "$year",
"avgs": {
"$push": {
"k": { "$substr": ["$month", 0, -1 ] },
"v": "$avgofozone"
}
}
} },
{ "$replaceRoot": {
"newRoot": {
"$mergeObjects": [
{ "$arrayToObject": "$avgs" },
"$$ROOT"
]
}
} },
{ "$project": { "avgs": 0 } }
], (err, data) => {
console.log("naresh:" +JSON.stringify(data));
const polute = Object.keys(data).reduce((p, c) => ({...p, monthsEnum[c]: data[c]}), {});
res.json(polute);
})

如果仅使用聚合管道进行整形,您可以使用 $let 运算符将月份索引映射到数组中的值。考虑运行以下管道:

const MONTHS = [, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

Light.aggregate([
{ "$match": {
"CREATE_DATE": {
"$lte": new Date(),
"$gte": new Date(new Date().setDate(new Date().getDate()-120))
}
} },
{ "$group": {
"_id": {
"month": { "$month": "$CREATE_DATE" },
"year": { "$year": "$CREATE_DATE" }
},
"avgofozone": { "$avg": "$OZONE" }
} },
{ "$group": {
"_id": "$year",
"avgs": {
"$push": {
"k": {
"$let": {
"vars": { "monthsList": MONTHS },
"in": { "$arrayElemAt": ["$$monthsList", "$month"] }
}
},
"v": "$avgofozone"
}
}
} },
{ "$replaceRoot": {
"newRoot": {
"$mergeObjects": [
{ "$arrayToObject": "$avgs" },
"$$ROOT"
]
}
} },
{ "$project": { "avgs": 0 } }
], (err, data) => {
console.log("naresh:" +JSON.stringify(data));
res.json(data);
})

关于javascript - 如何展平聚合输出的 JSON 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49463791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com