【发布时间】:2019-12-02 17:21:07
【问题描述】:
我有不同日期的数据切片,例如:
[{
"_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
}]
这些是针对一个广告系列但针对不同日期的相同报告。我想得到字段 clicks 和 attributedUnitsOrdered14d 的总和并将其作为单个对象返回,如下所示:
{
"_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 查询的预挂钩)或类似的东西吗?
【问题讨论】:
-
可能有一种方法可以使用 lodash 实现我需要的结果?
标签: javascript node.js mongodb mongoose mongodb-query