MongoDB:计算每个不同值的数量?

时间:2016-03-09 00:00:40

标签: javascript mongodb aggregation-framework

我有一组文件,其中包含不同项目的反馈列表。它看起来像这样:

{
  {
    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.

我想知道通过查看相同字段的不同值来总结事情的正确方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

您非常接近,但当然$eq只返回l2 = l[1:] l3 = l[1:4] 值,因此要创建该数字,您需要$cond

true/false

作为“三元”运算符db.collection(collectionName).aggregate([ { "$group" : { "_id": "$item", "good_count": { "$sum": { "$cond": [ { "$eq": [ "$rating", "good" ] }, 1, 0] } }, "neutral_count":{ "$sum": { "$cond": [ { "$eq": [ "$rating", "neutral" ] }, 1, 0 ] } }, "bad_count": { "$sum": { "$cond": [ { "$eq": [ "$rating", "bad" ] }, 1, 0 ] } } }} ]) 采用逻辑条件作为它的第一个参数(if)然后返回第二个参数,其中评估为$cond(然后)或第三个参数,其中{{1} 1}}(否则)。这使得true分别返回falsetrue/false以提供给1

另请注意,“案例”对0敏感。如果您有变量大小写,那么您可能希望在表达式中使用$toLower

$sum

稍微不同的是,以下聚合通常对不同的可能值更灵活,并且在性能方面围绕条件总和运行:

$eq

相反,这会产生如下输出:

               "$cond": [ { "$eq": [ { "$toLower": "$rating" },  "bad" ] }, 1, 0 ]

它们都是相同的信息,但您不必明确地匹配这些值,并且它确实以这种方式执行得更快。

相关问题