【发布时间】:2016-03-09 08:58:29
【问题描述】:
我有一组文档,其中包含针对不同项目的反馈列表。它看起来像这样:
{
{
item: "item_1"
rating: "neutral"
comment: "some comment"
},
{
item: "item_2"
rating: "good"
comment: "some comment"
},
{
item: "item_1"
rating: "good"
comment: "some comment"
},
{
item: "item_1"
rating: "bad"
comment: "some comment"
},
{
item: "item_3"
rating: "good"
comment: "some comment"
},
}
我想要一种方法来找出每个项目有多少不同的评分。
所以输出应该是这样的:
{
{
item: "item_1"
good: 12
neutral: 10
bad: 67
},
{
item: "item_2"
good: 2
neutral: 45
bad: 8
},
{
item: "item_3"
good: 1
neutral: 31
bad: 10
}
}
这就是我所做的
db.collection(collectionName).aggregate(
[
{
$group:
{
_id: "$item",
good_count: {$sum: {$eq: ["$rating", "Good"]}},
neutral_count:{$sum: {$eq: ["$rating", "Neutral"]}},
bad_count:{$sum: {$eq: ["$rating", "Bad"]}},
}
}
]
)
输出的格式看起来正确,但计数始终为 0。
我想知道通过查看同一字段的不同值来总结事情的正确方法是什么?
谢谢!
【问题讨论】:
标签: javascript mongodb aggregation-framework