【发布时间】:2018-09-02 23:09:02
【问题描述】:
使用 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
}
]
【问题讨论】:
标签: javascript node.js mongodb mongoose aggregation-framework