gpt4 book ai didi

javascript - 如何合并和求和 MongoDB 的结果

转载 作者:行者123 更新时间:2023-12-01 00:24:32 25 4
gpt4 key购买 nike

我有按不同日期划分的数据,例如:

[{
"_id" : ObjectId("5de4103552f7535d31c2e3e1"),
"attributedUnitsOrdered14d" : 0,
"clicks" : 1,
"targetId" : NumberLong(209434056899554),
"campaignId" : NumberLong(106574821410524),
"query" : "miami hurricanes t shirt",
"targetingType" : "TARGETING_EXPRESSION_PREDEFINED",
"campaignName" : "Miami Hurricanes - Sponsored Products - Auto",
"targetingExpression" : "close-match",
"adGroupId" : NumberLong(141291184479487),
"targetingText" : "close-match",
"date" : ISODate("2019-10-07T00:00:00.000Z"),
"__v" : 0
},
{
"_id" : ObjectId("5de4104b52f7535d31c2e3e7"),
"attributedUnitsOrdered14d" : 1,
"clicks" : 1,
"targetId" : NumberLong(209434056899554),
"campaignId" : NumberLong(106574821410524),
"query" : "miami hurricanes t shirt",
"targetingType" : "TARGETING_EXPRESSION_PREDEFINED",
"campaignName" : "Miami Hurricanes - Sponsored Products - Auto",
"targetingExpression" : "close-match",
"adGroupId" : NumberLong(141291184479487),
"targetingText" : "close-match",
"date" : ISODate("2019-10-08T00:00:00.000Z"),
"__v" : 0
},
{
"_id" : ObjectId("5de4105952f7535d31c2e3eb"),
"attributedUnitsOrdered14d" : 1,
"clicks" : 1,
"targetId" : NumberLong(209434056899554),
"campaignId" : NumberLong(106574821410524),
"query" : "miami hurricanes t shirt",
"targetingType" : "TARGETING_EXPRESSION_PREDEFINED",
"campaignName" : "Miami Hurricanes - Sponsored Products - Auto",
"targetingExpression" : "close-match",
"adGroupId" : NumberLong(141291184479487),
"targetingText" : "close-match",
"date" : ISODate("2019-10-09T00:00:00.000Z"),
"__v" : 0
}]

这些报告针对一个广告系列但针对不同日期。我想获取字段 clicksattributedUnitsOrdered14d 的总和并将其作为单个对象返回,如下所示:

{
"_id" : ...,
"attributedUnitsOrdered14d" : 2, // Sum of all the 3 objects
"clicks" : 3, // Sum of all the 3 objects
"targetId" : NumberLong(209434056899554),
"campaignId" : NumberLong(106574821410524),
"query" : "miami hurricanes t shirt",
"targetingType" : "TARGETING_EXPRESSION_PREDEFINED",
"campaignName" : "Miami Hurricanes - Sponsored Products - Auto",
"targetingExpression" : "close-match",
"adGroupId" : NumberLong(141291184479487),
"targetingText" : "close-match",
"date" : ISODate("2019-10-07T00:00:00.000Z"),
}

我可以使用中间件(find 查询的预 Hook )或类似的东西吗?

最佳答案

看起来很简单 $group应该足够了:

db.collection.aggregate([
{
$group: {
_id: null, //decide on different grouping id if needed.
attributedUnitsOrdered14d: {$sum: "$attributedUnitsOrdered14d"},
clicks: {$sum: "$clicks"},
date: {$last: "$date"}, //assuming data is sorted, if not add $sort before $group
targetId: {$first: "$targetId"},
campaignId: {$first: "$campaignId"},
query: {$first: "$query"},
targetingType: {$first: "$targetingType"},
campaignName: {$first: "$campaignName"},
targetingExpression: {$first: "$targetingExpression"},
adGroupId: {$first: "$adGroupId"},
targetingText: {$first: "$targetingText"},
}
}
])

关于javascript - 如何合并和求和 MongoDB 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59135542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com