MongoDB:计算每个不同值的数量? [英] MongoDB: Counting how many of each distinct values there are?

查看:873
本文介绍了MongoDB:计算每个不同值的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a collection of documents holding a list of feedbacks for different items. It looks something like this:

{
  {
    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"
  },
}

我想知道每个项目有多少不同的评分。

I want a way to find out how many different ratings each item got.

所以输出应该如下所示:

so the output should look something like this:

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

The format of the output looks right, but the counts are always 0.

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

I'm wondering what's the properway of summing things up by looking at the distinct values of the same field?

谢谢!

推荐答案

你非常接近,但当然 $ eq 只返回 true / false value,所以要使这个数字需要 $ cond

You were very close, but of course $eq just returns a true/false value, so to make that numeric you need $cond:

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 ]
           }
       }
  }}
])

作为三元运算符 $ cond 采用逻辑条件作为它的第一个参数(if),然后返回第二个参数,其中评估为 true (然后)或第三个参数,其中 false (else)。这使 true / false 返回 1 0 返回分别输入 $ sum

As a "ternary" operator $cond takes a logical condition as it's first argument (if) and then returns the second argument where the evaluation is true (then) or the third argument where false (else). This makes true/false returns into 1 and 0 to feed to $sum respectively.

另请注意,case对 $敏感当量。如果您有varing case,那么您可能需要 $ toLower 表达式中:

Also note that "case" is sensitive for $eq. If you have varing case then you likely want $toLower in the expressions:

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



< hr>

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


On a slightly different note, the following aggregation is usually more flexible to different possible values and runs rings around the conditional sums in terms of performance:

db.collection(collectionName).aggregate([
    { "$group": {
        "_id": { 
            "item": "$item",
            "rating": { "$toLower": "$rating" }
        },
        "count": { "$sum": 1 }
    }},
    { "$group": {
        "_id": "$_id.item",
        "results": {
            "$push": {
                "rating": "$_id.rating",
                "count": "$count"
            }
        }
    }}
])

相反给出如下输出:

{
    "_id": "item_1"
    "results":[
        { "rating": "good", "count": 12 },
        { "rating": "neutral", "count": 10 }
        { "rating": "bad", "count": 67 }
    ]
}

全部是相同的信息,但你没有明确匹配这些值,它确实以这种方式执行得更快。

It's all the same information, but you did not have to explicitly match the values and it does execute much faster this way.

这篇关于MongoDB:计算每个不同值的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆