【问题标题】:mongoose aggregate count by different criterias and group them猫鼬按不同标准聚合计数并将它们分组
【发布时间】:2021-07-30 04:50:07
【问题描述】:

我在 mongoDB 的集合中有大约 1000 个以下格式的文档:

{
 isOrder:true,
 item:
      {
       location:'A',
       type:'AA'
      }
 }

其中 isOrder 是布尔值,位置可以是 A 或 B,类型可以是 AA、AB、BB、BA 等。位置和类型是字符串。 我想按类型分组,并按其他两个属性计算项目。 像这样:

[
{
 type:'AA',
 orderA: <count of orders in location A>,
 orderB: <count of orders in location B>,
 stockA: <count of non-orders in location A>,
 stockB: <count of non-orders in location B>,
},
 type:'AB',
 orderA: <count of orders in location A>,
 orderB: <count of orders in location B>,
 stockA: <count of non-orders in location A>,
 stockB: <count of non-orders in location B>,
}
]

我整天都在尝试聚合、匹配、刻面... 我是最接近方面的,我能够获得最后 4 个属性,但没有按类型分组...... 请帮忙

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    你可以试试这个查询:

    基本思路是:

    • type分组
    • 如果条件匹配,则使用$sum$cond 添加1 或0。每个键值都有不同的条件
    db.collection.aggregate([
      {
        "$group": {
          "_id": "$item.type",
          "orderA": {
            "$sum": {
              "$cond": [
                {
                  "$and": [
                    {
                      "$eq": ["$isOrder",true]
                    },
                    {
                      "$eq": ["$item.location","A"]
                    }
                  ]
                },
                1,
                0
              ]
            }
          },
          "orderB": {
            "$sum": {
              "$cond": [
                {
                  "$and": [
                    {
                      "$eq": ["$isOrder",true]
                    },
                    {
                      "$eq": ["$item.location","B"]
                    }
                  ]
                },
                1,
                0
              ]
            }
          },
          "stockA": {
            "$sum": {
              "$cond": [
                {
                  "$and": [
                    {
                      "$eq": ["$isOrder",false]
                    },
                    {
                      "$eq": ["$item.location","A"]
                    }
                  ]
                },
                1,
                0
              ]
            }
          },
          "stockB": {
            "$sum": {
              "$cond": [
                {
                  "$and": [
                    {
                      "$eq": ["$isOrder",false]
                    },
                    {
                      "$eq": ["$item.location","B"]
                    }
                  ]
                },
                1,
                0
              ]
            }
          }
        }
      }
    ])
    

    例如here

    【讨论】:

    • 非常感谢...想不出$cond ..我正要跳过我的应用程序的这个功能...关于stackowerflow的第一个问题:D现在一切都说得通了,因为我看着你查询
    • 不客气,希望对您有所帮助。此外,如果查询正常,请将答案标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 2020-03-13
    • 2017-02-27
    • 2018-08-22
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    相关资源
    最近更新 更多