<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:计算每个不同的值有多少?

        时间:2023-10-02

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

                  上一篇:在投影中将子字段提升到顶层而不列出所有键 下一篇:$filter 在 mongodb 中最多 3 个嵌套级别

                  相关文章

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

                3. <tfoot id='u5SoY'></tfoot>
                    <bdo id='u5SoY'></bdo><ul id='u5SoY'></ul>

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

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