如何根据 MONgoDB 聚合中的条件查找计数? [英] How to find count based on condition in MOngoDB Aggregation?

查看:86
本文介绍了如何根据 MONgoDB 聚合中的条件查找计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取不同的 categoryCode 和 categoryName 并使用这种重复文档数量的组合来打印计数.这是匹配操作的一种条件,获取基于 cat_type 的所有文档,如果 cat_type 不存在,则在类别响应计数中显示为 0 样本数据:

<预><代码>[{cat_id":1,类别代码":类别代码1",类别名称":类别名称1",猫":[{类型":A"},{类型":B"},{类型":C"}]},{cat_id":2,类别代码":类别代码1",类别名称":类别名称1",猫":[{类型":A"}]},{cat_id":3,categoryCode":categoryCode2",类别名称":类别名称2",猫":[{类型":C"}]},{cat_id":4,categoryCode":categoryCode3","categoryName":"categoryName3",猫":[{类型":A"}]}]

预期输出:此处与 type='A' 匹配,category1 和 category3 具有 type='A' 然后计数正常打印但 category2 没有此 cat_type 然后 category2 也显示为 cat_count=0 的响应

<预><代码>[{类别代码":类别代码1",类别名称":类别名称1",cat_count":2},{categoryCode":categoryCode2",类别名称":类别名称2",cat_count":0},{categoryCode":categoryCode3","categoryName":"categoryName3",cat_count":1}]

我正在使用查询 - 但查询可以使用外部字段,但不能在基于数组的条件内使用

db.collection.aggregate([{$组":{_id":{"categoryCode":"$categoryCode",categoryName":$categoryName"},猫计数":{$sum":{$cond":{如果":{$eq":[$cat.type",A"]},然后":1,其他":0}}}}},{$项目":{类别代码":$_id.categoryCode","categoryName":"$_id.categoryName",catCount":1,_id":0}}])

解决方案

只需要使用$in操作符条件代替$eq操作符条件,

<代码> {$组:{_ID: {categoryCode":$categoryCode",categoryName":$categoryName"},猫计数":{$sum: {$cond: [{ $in: [A", $cat.type"] }, 1, 0]}}}}

游乐场

I need to get the distinct categoryCode and categoryName and print the count with this combination of how many documents are repeated. here is one condition with match operation gets all the documents based on cat_type , if cat_type does not exist then in category response count show as 0 sample Data:

[
   {
      "cat_id":1,
      "categoryCode":"categoryCode1",
      "categoryName":"categoryName1",
      "cat":[
         {
            "type":"A"
         },
         {
            "type":"B"
         },
         {
            "type":"C"
         }
      ]
   },
   {
      "cat_id":2,
      "categoryCode":"categoryCode1",
      "categoryName":"categoryName1",
      "cat":[
         {
            "type":"A"
         }
      ]
   },
   {
      "cat_id":3,
      "categoryCode":"categoryCode2",
      "categoryName":"categoryName2",
      "cat":[
         {
            "type":"C"
         }
      ]
   },
   {
      "cat_id":4,
      "categoryCode":"categoryCode3",
      "categoryName":"categoryName3",
      "cat":[
         {
            "type":"A"
         }
      ]
   }
]

Expected Output: here match with type='A', category1 and category3 have type='A' then count is print as normal but category2 does not have this cat_type then category2 also show in response with cat_count=0

[
   {
      "categoryCode":"categoryCode1",
      "categoryName":"categoryName1",
      "cat_count": 2
   },
   {
      "categoryCode":"categoryCode2",
      "categoryName":"categoryName2",
      "cat_count": 0
   },
   {
      "categoryCode":"categoryCode3",
      "categoryName":"categoryName3",
      "cat_count": 1
   }
]

I am using query- but query work with the outside field but not work inside array based condition

db.collection.aggregate([
   {
      "$group":{
         "_id":{
            "categoryCode":"$categoryCode",
            "categoryName":"$categoryName"
         },
         "catCount":{
            "$sum":{
               "$cond":{
                  "if":{
                     "$eq":[
                        "$cat.type",
                        "A"
                     ]
                  },
                  "then":1,
                  "else":0
               }
            }
         }
      }
   },
   {
      "$project":{
         "categoryCode":"$_id.categoryCode",
         "categoryName":"$_id.categoryName",
         "catCount":1,
         "_id":0
      }
   }
])

解决方案

Just need to use $in operator condition instead of $eq operator condition,

  {
    $group: {
      _id: {
        "categoryCode": "$categoryCode",
        "categoryName": "$categoryName"
      },
      "cat_count": {
        $sum: {
          $cond: [{ $in: ["A", "$cat.type"] }, 1, 0]
        }
      }
    }
  }

Playground

这篇关于如何根据 MONgoDB 聚合中的条件查找计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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