<bdo id='optDO'></bdo><ul id='optDO'></ul>
<legend id='optDO'><style id='optDO'><dir id='optDO'><q id='optDO'></q></dir></style></legend>

  • <tfoot id='optDO'></tfoot>

    <i id='optDO'><tr id='optDO'><dt id='optDO'><q id='optDO'><span id='optDO'><b id='optDO'><form id='optDO'><ins id='optDO'></ins><ul id='optDO'></ul><sub id='optDO'></sub></form><legend id='optDO'></legend><bdo id='optDO'><pre id='optDO'><center id='optDO'></center></pre></bdo></b><th id='optDO'></th></span></q></dt></tr></i><div id='optDO'><tfoot id='optDO'></tfoot><dl id='optDO'><fieldset id='optDO'></fieldset></dl></div>
      1. <small id='optDO'></small><noframes id='optDO'>

        MongoDB:计算每个不同的值有多少?

        MongoDB: Counting how many of each distinct values there are?(MongoDB:计算每个不同的值有多少?)

      2. <tfoot id='1jOVQ'></tfoot>
          <i id='1jOVQ'><tr id='1jOVQ'><dt id='1jOVQ'><q id='1jOVQ'><span id='1jOVQ'><b id='1jOVQ'><form id='1jOVQ'><ins id='1jOVQ'></ins><ul id='1jOVQ'></ul><sub id='1jOVQ'></sub></form><legend id='1jOVQ'></legend><bdo id='1jOVQ'><pre id='1jOVQ'><center id='1jOVQ'></center></pre></bdo></b><th id='1jOVQ'></th></span></q></dt></tr></i><div id='1jOVQ'><tfoot id='1jOVQ'></tfoot><dl id='1jOVQ'><fieldset id='1jOVQ'></fieldset></dl></div>

          <small id='1jOVQ'></small><noframes id='1jOVQ'>

              <tbody id='1jOVQ'></tbody>

          1. <legend id='1jOVQ'><style id='1jOVQ'><dir id='1jOVQ'><q id='1jOVQ'></q></dir></style></legend>
              • <bdo id='1jOVQ'></bdo><ul id='1jOVQ'></ul>
                1. 本文介绍了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
                    }
                  
                  }
                  

                  This is what I've done

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

                  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?

                  Thanks!

                  解决方案

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

                  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.

                  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 ]
                  


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

                  That would instead give output like this:

                  {
                      "_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:计算每个不同的值有多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                2. <legend id='u5SoY'><style id='u5SoY'><dir id='u5SoY'><q id='u5SoY'></q></dir></style></legend>

                  • <tfoot id='u5SoY'></tfoot>
                      <bdo id='u5SoY'></bdo><ul id='u5SoY'></ul>

                      <small id='u5SoY'></small><noframes id='u5SoY'>

                          • <i id='u5SoY'><tr id='u5SoY'><dt id='u5SoY'><q id='u5SoY'><span id='u5SoY'><b id='u5SoY'><form id='u5SoY'><ins id='u5SoY'></ins><ul id='u5SoY'></ul><sub id='u5SoY'></sub></form><legend id='u5SoY'></legend><bdo id='u5SoY'><pre id='u5SoY'><center id='u5SoY'></center></pre></bdo></b><th id='u5SoY'></th></span></q></dt></tr></i><div id='u5SoY'><tfoot id='u5SoY'></tfoot><dl id='u5SoY'><fieldset id='u5SoY'></fieldset></dl></div>
                              <tbody id='u5SoY'></tbody>