【问题标题】:In Mongo aggregation can 3 different counts be generated with grouping在 Mongo 聚合中,可以通过分组生成 3 个不同的计数
【发布时间】:2022-09-23 00:51:51
【问题描述】:

目前,我有 3 个不同的聚合查询,它们根据 companyRegNo 的分组生成不同的计数。有没有办法结合这三个查询。

employeeActions 数据如下所示:

{   
    \"email\": \"one@gmail.com\",
    \"companyRegNo\" : 105,    
    \"event\" : {
        \"created\" : ISODate(\"2022-09-16T06:42:42.761Z\"),
        \"desc\" : \"COMPLETED_APPLICATIONS\",
        \"note\" : \"Direct apply\",       
    }
}
{   
    \"email\": \"one@gmail.com\",
    \"companyRegNo\" : 247,
    \"event\" : {
        \"created\" : ISODate(\"2022-09-16T06:42:04.387Z\"),       
        \"desc\" : \"COMPLETED_APPLICATIONS\",
        \"note\" : \"\",       
    }
}
{   
    \"email\": \"one@gmail.com\",
    \"companyRegNo\" : 247,    
    \"event\" : {
        \"created\" : ISODate(\"2022-09-16T06:42:42.761Z\"),
        \"desc\" : \"COMPLETED_REVIEW\",
        \"note\" : \"\"Sent for review\"\",       
    }
}
{   
    \"email\": \"one@gmail.com\",
    \"companyRegNo\" : 105,    
    \"event\" : {
        \"created\" : ISODate(\"2022-09-16T06:42:42.761Z\"),
        \"desc\" : \"COMPLETED_REVIEW\",
        \"note\" : \"Data is complete\",       
    }
}
{   
    \"email\": \"one@gmail.com\",
    \"companyRegNo\" : 247,
    \"event\" : {
        \"created\" : ISODate(\"2022-09-16T06:42:04.387Z\"),       
        \"desc\" : \"COMPLETED_OFFER\",
        \"note\" : \"\",       
    }
}
{   
    \"email\": \"one@gmail.com\",
    \"companyRegNo\" : 227,
    \"event\" : {
        \"created\" : ISODate(\"2022-09-16T06:42:04.387Z\"),       
        \"desc\" : \"COMPLETED_APPLICATIONS\",
        \"note\" : \"\",       
    }
}
{   
    \"email\": \"two@gmail.com\",
    \"companyRegNo\" : 227,    
    \"event\" : {
        \"created\" : ISODate(\"2022-09-16T06:42:42.761Z\"),
        \"desc\" : \"COMPLETED_APPLICATIONS\",
        \"note\" : \"\",       
    }
}

{   
    \"email\": \"two@gmail.com\",
    \"companyRegNo\" : 105,    
    \"event\" : {
        \"created\" : ISODate(\"2022-09-16T06:42:42.761Z\"),
        \"desc\" : \"COMPLETED_APPLICATIONS\",
        \"note\" : \"Direct apply\",       
    }
}

3个不同的查询是:

查询评论计数

db.getCollection(\'employeeActions\').aggregate([
{\"$match\": { 
    \"event.created\": {\"$gte\": ISODate(\"2022-06-01 00:00:00.000Z\")},
    \"$or\": [ {\"event.desc\": \"COMPLETED_REVIEW\"}, {\"event.note\": \"Sent for review\"}],
}},
{\"$group\":{\"_id\":\"$companyRegNo\",\"count\": {\"$sum\": 1 } } },
{\"$project\":{ \"companyRegNo\":\"$_id\",\"count\": \"$count\",\"_id\":0}}
])

结果将是

105    1  
227    0  
247    1  

查询报价计数

db.getCollection(\'employeeActions\').aggregate([
{\"$match\": { 
    \"event.created\": {\"$gte\": ISODate(\"2022-06-01 00:00:00.000Z\")},
    \"event.desc\": \"COMPLETED_OFFER\"
}},
{\"$group\":{\"_id\":\"$companyRegNo\",\"count\": {\"$sum\": 1 } } },
{\"$project\":{ \"companyRegNo\":\"$_id\",\"count\": \"$count\",\"_id\":0}},
])

结果将是

105    0  
227    0  
247    1  

查询应用程序计数

db.getCollection(\'employeeActions\').aggregate([
{\"$match\": { 
    \"event.created\": {\"$gte\": ISODate(\"2022-06-01 00:00:00.000Z\")},
    \"event.desc\": \"COMPLETED_APPLICATIONS\"
}},
{\"$group\":{\"_id\":\"$companyRegNo\",\"count\": {\"$sum\": 1 } } },
{\"$project\":{ \"companyRegNo\":\"$_id\",\"count\": \"$count\",\"_id\":0}},
])

结果将是

105    2
227    2
247    1

有没有办法将这 3 个查询结合起来,以便得到如下结果?

CompanyRegNo 应用程序 评论 优惠

105            2           1         0
227            2           0         0
247            1           1         1

    标签: mongodb aggregate grouping


    【解决方案1】:

    是的,您可以使用$facet 阶段同时处理多个聚合管道:

    { 
      $facet: {
         count_1: [ // Your first query ],
         count_2: [ // Your second query ],
         count_3: [ // Your third query ],
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      相关资源
      最近更新 更多