MongoDB:通过计算每个字段的平均值将对象数组简化为单个对象 [英] MongoDB: Reduce array of objects into a single object by computing the average of each field
问题描述
我使用MongoDB聚合API每天聚合一些数据. 这种聚合的结果具有以下格式:
I use the MongoDB aggregation API to aggregate some data daily. The result of this aggregation is of this format:
[
{
aggDate: '2019-05-23',
results: [
{
foo: 0.58,
bar: 0.42
}, {
foo: 0.32,
bar: 0.98
}
]
}
]
关于日期的聚合是可以的,但是现在我想聚合results
数组中的对象.
The aggregation on the date is fine, but now I would like to aggregate the objects in the results
array.
此聚合的结果应采用以下格式:
The result of this aggregation should be of the following format:
[
{
aggDate: '2019-05-23',
result: {
foo: 0.45 // avg of all the `foo`, here: (0.58 + 0.32) / 2
bar: 0.7 // avg of all the `bar`, here: (0.42 + 0.98) / 2
}
}
]
我的问题是键foo
和bar
可以更改/可以在results
对象中添加新字段.为了避免每次发生查询时都重新编码,我想使用某种通用的方式对MongoDB说
My problem here is that the keys foo
and bar
can change/new fields can be added in results
objects. To avoid recoding the query each time it occurs, I want to use some generic way to say to MongoDB
采用此对象数组并将其简化为单个对象,其中每个值都是所有对象中同一字段的平均值.
Take this array of objects and reduce it into a single object where each value is the average of the same field in all objects.
我知道$reduce
运算符存在于MongoDB中,但是我不知道如何使用它,甚至不确定它是否可以在这里为我提供帮助.
I know the $reduce
operator exists in MongoDB but I can't figure out how to use it and I am not even sure if it can help me here.
推荐答案
您应运行 $ unwind 并使用 $ group 阶段.您还需要 $ arrayToObject 和 $ objectToArray 以使用动态键. $ reduce 在这里不是选项,因为键是unknwon
You should run $unwind and aggregate the data using $group stages. You also need $arrayToObject and $objectToArray to work with dynamic keys. $reduce is not an option here since keys are unknwon
db.col.aggregate([
{
$project: {
aggDate: 1,
results: {
$map: { input: "$results", in: { $objectToArray: "$$this" } }
}
}
},
{
$unwind: "$results"
},
{
$unwind: "$results"
},
{
$group: {
_id: { aggDate: "$aggDate", k: "$results.k" },
sum: { $sum: "$results.v" },
count: { $sum: 1 }
}
},
{
$project: {
_id: 1,
v: { $divide: [ "$sum", "$count" ] }
}
},
{
$group: {
_id: "$_id.aggDate",
results: { $push: { k: "$_id.k", v: "$v" } }
}
},
{
$project: {
_id: 0,
aggDate: "$_id",
results: { $arrayToObject: "$results" }
}
}
])
这篇关于MongoDB:通过计算每个字段的平均值将对象数组简化为单个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!