从mongodb中另一个集合的foreignField上使用$ group进行分组

时间:2017-04-28 16:15:30

标签: mongodb mongodb-query aggregation-framework

这是我的疑问:

db.getCollection('_build').aggregate([
    {
        $group:{
            _id: "$ProjectId",
            Builds: {$addToSet: "$_id"}
        }
    },
    {
        $lookup:{
            from: "_build.detail",
            localField: "Builds",
            foreignField: "BuildId",
            as: "result"
        }
    },
    {
        $project:{
            _id:0,
            ProjectId: "$_id",
            Builds: 1,
            BuildCountForProject: {$size: "$Builds"}
        }
    }

]);

查询结果:

/* 1 */
{
    "Builds" : [ 
        ObjectId("58f908411f19cf1d340974c2"), 
        ObjectId("58f902bd1f19cf1d3409749d")
    ],
    "ProjectId" : ObjectId("58f7a38f1f19cf38306a1b9c"),
    "BuildCountForProject" : 11
}

/* 2 */
{
    "Builds" : [ 
        ObjectId("58f797631091c228bc3af071"), 
        ObjectId("58f79fa31091c528bc4ff8f1"), 
        ObjectId("58f769441f19cf22dc92da24"), 
        ObjectId("58f633801f19cf4f8073b203")
    ],
    "ProjectId" : ObjectId("58e27c921091c22e34243db7"),
    "BuildCountForProject" : 4
}

我在查询结果中针对每个项目获取Builds数组,我想在每个项目的Builds的基础上查询另一个集合(使用相同的查询),想要聚合一些数据(Data必须是按ProjectId分组,我没有想要加入的集合中有ProjectId,但是我已经将它放在管道中,你可以在结果中看到。)

示例:

我想找到这些版本的广告,广告,广告系列的独特数量,

  1. 的ObjectId(" 58f908411f19cf1d340974c2&#34)
  2. 的ObjectId(" 58f902bd1f19cf1d3409749d&#34)
  3. 这两个版本都属于项目ObjectId(" 58f7a38f1f19cf38306a1b9c"),如结果所示。

    想要输出,

    {
        "ProjetId" : ObjectId("58f7a38f1f19cf38306a1b9c"),
        "BuildId" :[ ObjectId("58f908411f19cf1d340974c2"), 
                    ObjectId("58f902bd1f19cf1d3409749d")]
        "UniqueAdsCount" : 10,
        "UniqueAdsetCount": 5,
        "UniqueCampaignCount": 2 
    },
    {
        next ProjectId,
        Builds array,
        UniqueAdsCount
        UniqueAdsetCount
        UniqueCampaignCount
    },...
    

    我希望加入with_build.detials:

    {
        "_id" : ObjectId("58de834cc6e7dbe945acf890"),
        "BuildId" : ObjectId("58ef4b95c6e7dbe945ba700b"),
        "Values" : null,
        "Headers" : null,
        "Data" : {
            "Campaign Name" : "Remarketing | Remarketing | Facebook | Conversions | 03-01-2017",
            "Ad Set Name" : "Cancelled Orders_Greater than 50%-Cancelled Orders_Less than 50% | Desktop | Feed | Female | 21-65",
            "Ad Name" : "Carousel | Draw1,Excited2,Lottery5,Beach4 | S:1814082498827964 | 03-01-2017 | 70Custom Audiences | ",
            "Ad Set Run Status" : "ACTIVE",
            "Ad Status" : "ACTIVE",
            "Campaign Objective" : "Conversions",
            "Gender" : "Female",
            "Age Min" : "21",
            "Age Max" : "65",
    
        },
        "Status" : false,
        "CampaignName" : "Remarketing | Remarketing | Facebook | Conversions | 03-01-2017",
        "AdSetName" : "Cancelled Orders_Greater than 50%-Cancelled Orders_Less than 50% | Desktop | Feed | Female | 21-65",
        "AdName" : "Carousel | Draw1,Excited2,Lottery5,Beach4 | S:1814082498827964 | 03-01-2017 | 70Custom Audiences | ",
        "Campaign_Status" : 1,
        "Campaign_Id" : "1",
        "Adset_Status" : 1,
        "Adset_Id" : "123",
        "Ad_Status" : 1,
        "Ad_Id" : "1234"
    },...
    

1 个答案:

答案 0 :(得分:1)

您可以尝试以下查询。它类似于我们在这里的查询Using multiple $lookup with aggregation in mongodb

以下查询将$lookup导入build details以获取所有字段,并$group一次为一个字段获取不同的值,然后$project计算不同的值每个版本。

最终$group是将所有builds推送到一个数组中,同时将所有构建详细信息的总和计算在一起。

   aggregate([
      { $group: {_id: "$ProjectId",Builds: {$addToSet: "$_id"}}},
      { $lookup: {from: "_build.detail",localField: "Builds",foreignField: "BuildId",as: "result"}},
      { $unwind: "$result"},
      {
        $group: 
         {
           _id: 
            {
              ProjectId: "$_id",
              BuildId: "$result.BuildId",
              campaignName: "$result.Data.Campaign Name",
              adSet: "$result.Data.Ad Set Name"
            },
            uniqueAdNames: {$addToSet: "$result.Data.Ad Name"}
          }
      },
      { $addFields: {uniqueAdsCount: {$size: "$uniqueAdNames"}}},
      {
        $group: 
          {
            _id: 
              {
                ProjectId: "$_id.ProjectId",
                BuildId: "$_id.BuildId",
                campaignName: "$_id.campaignName"
              },
              uniqueAdsCount: {$first: "$uniqueAdsCount"},
              uniqueAdSets: {$addToSet: "$_id.adSet"}
          }
      },
      { $addFields: {uniqueAdsetCount: {$size: "$uniqueAdSets"}}},
      {
        $group: 
          {
            _id:{ProjectId: "$_id.ProjectId", BuildId: "$_id.BuildId"},
            uniqueAdsCount: {$first: "$uniqueAdsCount"},
            uniqueAdsetCount: {$first: "$uniqueAdsetCount"},
            uniqueCampaignNames: {$addToSet: "$_id.campaignName"}
          }
      },
      { $addFields: {uniqueCampaignCount: {$size: "$uniqueCampaignCount"}}},
      {
        $group: 
         {
           _id: "$projectId",
           Builds: {$push: "$_id.BuildId"},
           uniqueAdsCount: {$sum: "$uniqueAdsCount"},
           uniqueAdsetCount: {$sum: "$uniqueAdsetCount"},
           uniqueCampaignCount: {$sum: "$uniqueCampaignCount"}
          }
      },
      {
        $project:
         {
           _id: 0,
           ProjectId: "$_id.ProjectId",
           Builds: 1,
           BuildCountForProject: {$size: "$Builds"},
           uniqueAdsCount: 1,
           uniqueAdsetCount: 1,
           uniqueCampaignCount: 1,
         }
      }
   ])