以下是我的mongodb系列中的三个文件,
[
{
"_id" : "58ad8a35ef2bf403cc4750ff1",
"Data" : {
"Campaign Name" : "Campaign 1",
"Ad Set Name" : "Adset 1.1",
"Ad Name" : "Ad 1.1.1"
}
},
{
"_id" : "58ad8a35ef2bf403cc4750ff2",
"Data" : {
"Campaign Name" : "Campaign 1",
"Ad Set Name" : "Adset 1.1",
"Ad Name" : "Ad 1.1.2"
}
},
{
"_id" : "58ad8a35ef2bf403cc4750ff3",
"Data" : {
"Campaign Name" : "Campaign 1",
"Ad Set Name" : "Adset 2",
"Ad Name" : "Ad 1.2.1"
}
}
]
注意:
我的文档中的“广告系列名称”字段定义了各种广告系列,然后我在每个广告系列中都有多个广告集(广告集名称),然后我在每个广告集中都有多个广告(广告名称)(广告)设置名称)表示每个广告必须属于一个广告集(广告集名称),每个广告集(广告集名称)必须属于广告系列(广告系列名称)
到目前为止我成功完成的任务:
db.adsets.aggregate([
{
$group:{
_id:"$Data.Campaign Name",
CampaignCount : {$sum:1},
UniqueAdsets: {$addToSet : "$Data.Ad Set Name"}
}
},
{
$project:{
_id:1,
CampaignCount: 1,
UniqueAdsets: 1,
UniqueAdsetCount:{$size:"$UniqueAdsets"}
}
}
]);
我已经针对每个独特广告系列及其计数找到了唯一广告系列(广告系列名称)和广告集(广告集名称),上面的查询为我提供了此结果,
[
{
"_id" : "Campaign 1",
"CampaignCount" : 60.0,
"UniqueAdsets" : [
"Adset 1",
"Adset 2",
"Adset 3",
"Adset 4"
],
"UniqueAdsetCount" : 4
},
{
"_id" : "Campaign 2",
"CampaignCount" : 60.0,
"UniqueAdsets" : [
"Adset 1",
"Adset 2"
],
"UniqueAdsetCount" : 2
}
]
我正在尝试修改此查询,以便它可以在我现有查询中找到的每个广告集(广告集名称)中为我提供广告(广告名称)和广告计数。 我想要这样的东西作为输出,
[
{
"_id" : "Campaign 1",
"CampaignCount" : 60.0,
"UniqueAdsets" : [
{"Adset 1":["ad1","ad2","ad3"],"adcount":3},
{"Adset 2":["ad1","ad2"],"adcount":2},
{"Adset 3":["ad1","ad2"],"adcount":2},
{"Adset 4":["ad1","ad2"],"adcount":2}
],
"UniqueAdsetCount" : 4
},
{
"_id" : "Campaign 2",
"CampaignCount" : 60.0,
"UniqueAdsets" : [
{"Adset 2":["ad1","ad2"],"adcount":2},
{"Adset 2":["ad1","ad2"],"adcount":2}
],
"UniqueAdsetCount" : 2
}
]
我现在正在尝试这个,但它没有用,
db.adsets.aggregate([
{
$group:{
_id:{"CampaignName":"$Data.Campaign Name"},
CampaignCount : {$sum:1},
UniqueAdsets: {$addToSet : "$Data.Ad Set Name"}
}
},
{
$group:{
_id:{"AdsetName":"$Data.Ad Set Name"},
UniqueAds: {$addToSet : "$Data.Ad Name"}
}
},
{
$project:{
_id:0,
CampaignName:"$_id.CampaignName",
CampaignCount: 1,
UniqueAdsets: 1,
UniqueAdsetCount:{$size:"$UniqueAdsets"},
UniqueAds:1,
UniqueAds:{$size:"$UniqueAds"}
}
}
]);
答案 0 :(得分:3)
您可以尝试以下聚合。
$group
:按Campaign Name
分组,将广告系列和$push
所有广告计入Adsets
数组。
$unwind
:展开Adsets
数组
$group
:将AdSetName
和$push
个所有广告组分组到AdName
数组中并计算广告。
$group
:Campaign Name
的最终小组,将所有内容重新投入到所需的回复中。
$project
:投放所有需要的字段,并为广告添加$size
db.adsets.aggregate([{
$group: {
_id: "$Data.Campaign Name",
CampaignCount: {
$sum: 1
},
Adsets: {
$push: {
AdSetName: "$Data.Ad Set Name",
AdName: "$Data.Ad Name"
}
}
}
}, {
$unwind: "$Adsets"
}, {
$group: {
_id: "$Adsets.AdSetName",
CampaignCount: {
$first: "$CampaignCount"
},
CampaignName: {
$first: "$_id"
},
AdsetCount: {
$sum: 1
},
AdName: {
$push: "$Adsets.AdName"
}
}
}, {
$group: {
_id: "$CampaignName",
CampaignCount: {
$first: "$CampaignCount"
},
AdSets: {
$push: {
AdSetName: "$_id",
AdName: "$AdName",
AdsetCount: "$AdsetCount"
}
}
}
}, {
$project: {
_id: 1,
CampaignCount: 1,
AdSets: 1,
AdsetCount: {
$size: "$AdSets"
}
}
}]);
答案 1 :(得分:1)
按照CampaignName,AdSet
$group
:按广告系列名称和广告集进行分组,选择适用于此组合的广告数量,以用于整个广告系列
$group
:第二个按广告系列名称分组,使用添加数组准备集合。使用先前选择的计数作为广告系列的总计数
$project
:项目需要具有设置计数的元素
db.adsets.aggregate([ {
$group : {
_id : {
"campaignName" : "$Data.Campaign Name",
"adSet" : "$Data.Ad Set Name",
},
ads : {
$addToSet : "$Data.Ad Name"
},
campaignSetAdCount : {
$sum : 1
}
}
}, {
$group : {
_id : "$_id.campaignName",
sets : {
$addToSet : {
adSet : "$_id.adSet",
ads : "$ads",
adCount : "$campaignSetAdCount",
uniqueAdCount : {
$size : "$ads"
}
}
},
campaginCount : {
$sum : "$campaignSetAdCount"
}
}
}, {
$project : {
_id : 0,
campaignName : "$_id",
campaginCount : 1,
uniqueSetCount : {
$size : "$sets"
},
sets : "$sets"
}
} ])