gpt4 book ai didi

javascript - MongoDB 汇总子文档上的每个键

转载 作者:可可西里 更新时间:2023-11-01 09:10:53 25 4
gpt4 key购买 nike

我有多个具有此架构的文档,每个文档每天针对每个产品:

{
_id:{},
app_id:'DHJFK67JDSJjdasj909',
date:'2014-08-07',
event_count:32423,
event_count_per_type: {
0:322,
10:4234,
20:653,
30:7562
}
}

我想获取特定日期范围内每个事件类型的总和。
这是我正在寻找的输出,其中每个事件类型已在所有文档中求和。 event_count_per_type 的键可以是任何东西,所以我需要一些可以循环遍历每个键的键,而不是必须隐含它们的名称。

{
app_id:'DHJFK67JDSJjdasj909',
event_count:324236456,
event_count_per_type: {
0:34234222,
10:242354,
20:456476,
30:56756
}
}

到目前为止,我已经尝试了几个查询,这是我迄今为止得到的最好的查询,但是子文档值没有求和:

db.events.aggregate(
{
$match: {app_id:'DHJFK67JDSJjdasj909'}
},
{
$group: {
_id: {
app_id:'$app_id',
},
event_count: {$sum:'$event_count'},
event_count_per_type: {$sum:'$event_count_per_type'}
}
},
{
$project: {
_id:0,
app_id:'$_id.app_id',
event_count:1,
event_count_per_type:1
}
}
)

我看到的输出是 event_count_per_type 键的值 0,而不是对象。我可以修改模式,使键位于文档的顶层,但这仍然意味着我需要在组语句中为每个键都有一个条目,因为我不知道键名是什么我不能做。

如有任何帮助,我将不胜感激,如果需要,我愿意更改我的模式,并尝试使用 mapReduce(尽管从文档来看,性能似乎很差。)

最佳答案

如前所述,聚合框架无法像这样处理文档,除非您实际上要提供所有键,例如:

db.events.aggregate([
{ "$group": {
"_id": "$app_id",
"event_count": { "$sum": "$event_count" },
"0": { "$sum": "$event_count_per_type.0" },
"10": { "$sum": "$event_count_per_type.10" }
"20": { "$sum": "$event_count_per_type.20" }
"30": { "$sum": "$event_count_per_type.30" }
}}
])

但是您当然必须明确指定您希望处理的每个键。这对于 MongoDB 中的聚合框架和一般查询操作都是如此,至于访问以这种“子文档”形式标记的元素,您需要指定元素的“确切路径”才能对其执行任何操作。

聚合框架和通用查询没有“遍历”的概念,这意味着它们无法处理文档的“每个键”。这需要一种语言结构才能执行这些接口(interface)中未提供的操作。

不过,一般来说,使用“键名”作为数据点,其名称实际上代表“值”是一种“反模式”。对此建模的更好方法是使用数组并将您的“类型”本身表示为一个值:

{
"app_id": "DHJFK67JDSJjdasj909",
"date: ISODate("2014-08-07T00:00:00.000Z"),
"event_count": 32423,
"events": [
{ "type": 0, "value": 322 },
{ "type": 10, "value": 4234 },
{ "type": 20, "value": 653 },
{ "type": 30, "value": 7562 }
]
}

还注意到“日期”现在是一个正确的日期对象而不是字符串,这也是一种很好的做法。这种数据虽然很容易用聚合框架处理:

db.events.aggregate([
{ "$unwind": "$events" },
{ "$group": {
"_id": {
"app_id": "$app_id",
"type": "$events.type"
},
"event_count": { "$sum": "$event_count" },
"value": { "$sum": "$value" }
}},
{ "$group": {
"_id": "$_id.app_id",
"event_count": { "$sum": "$event_count" },
"events": { "$push": { "type": "$_id.type", "value": "$value" } }
}}
])

这显示了一个两阶段分组,首先获取每个“类型”的总计而不指定每个“键”,因为您不再需要,然后返回每个“app_id”的单个文档,结果在数组中原来存储的。这种数据形式通常对于查看特定“类型”甚至特定范围内的“值”要灵活得多。

在您无法更改结构的情况下,您唯一的选择就是 mapReduce。这允许您“编码”键的遍历,但由于这需要 JavaScript 解释和执行,因此它不如聚合框架快:

db.events.mapReduce(
function() {
emit(
this.app_id,
{
"event_count": this.event_count,
"event_count_per_type": this.event_count_per_type
}
);
},
function(key,values) {

var reduced = { "event_count": 0, "event_count_per_type": {} };

values.forEach(function(value) {
for ( var k in value.event_count_per_type ) {
if ( !redcuced.event_count_per_type.hasOwnProperty(k) )
reduced.event_count_per_type[k] = 0;
reduced.event_count_per_type += value.event_count_per_type;
}
reduced.event_count += value.event_count;
})
},
{
"out": { "inline": 1 }
}
)

这将实质上遍历和组合“键”,并对找到的每个键的值求和。

所以你的选择是:

  1. 更改结构并使用标准查询和聚合。
  2. 保持结构并需要 JavaScript 处理和 mapReduce。

这取决于您的实际需求,但在大多数情况下重组会产生 yield 。

关于javascript - MongoDB 汇总子文档上的每个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25187453/

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