【发布时间】:2020-06-05 16:16:45
【问题描述】:
以下聚合在 mongo db 中工作,但是当我尝试将其与节点 js 集成以通过API 获取计数值时,它返回 null 值
我目前正在寻找在Node js 中执行Aggregation 的方法。
下面是node js的集成代码
app.get('/count', (req, res) => {
var name = req.params.name;
adpostdata.aggregate([
{ "$facet": {
"Total": [
{ "$match" : { "category": { "$exists": true }}},
{ "$count": "Total" },
]
}},
{ "$project": {
"Total": { "$arrayElemAt": ["$Total.Total", 0] },
}}
], (err, result) => {
if (err) return console.log(err)
console.log(result)
res.send(result+"")
})
})
我的输入是
{
category :"dress"
}
{
category:"cars"
}
【问题讨论】: