mongodb - 在聚合管道期间对列表索引进行平均?
问题描述
我目前有一个 MongoDB 聚合管道,它以以下类型的合成文档结束
[
{
'_id': '2019-09-10',
'grouped_foos':
[
{... 'foo': [1, 78, 100]},
{... 'foo': [8, 66, 98]},
{... 'foo': [99, 5, 33]},
{... 'foo': [120, 32, 2]}
]
},
{
'_id': '2019-09-09',
'grouped_foos':
[
{... 'foo': [10, 27]},
{... 'foo': [19, 66]}
]
},
{
'_id': '2019-09-08',
'grouped_foos':
[
{... 'foo': [1]}
]
}
]
我想继续这个管道并将foo
列表的索引平均在一起以形成看起来像的文档
[
{
'_id': '2019-09-10',
'avg_foo': [57, 45.25, 58.25]
},
{
'_id': '2019-09-09',
'avg_foo': [14.5, 46.5]
},
{
'_id': '2019-09-08',
'avg_foo': [1]
}
]
在聚合期间是否可以进行这种类型的平均?我是否可能需要$unwind
索引列表并分配新_id
的唯一性以使文档看起来像
[
{
'_id': UUID,
'old_id': '2019-09-10',
'foo': 1,
'index': 0
},
{
'_id': UUID,
'old_id': '2019-09-10',
'foo': 78,
'index': 1
},
........
]
解决方案
基本上你可以尝试$unwind
但更简单和更快的方法是使用$reduce到$map和$sum中的所有行grouped_foos
。然后你就可以运行另一个$map并使用$divide来获得平均值。
db.collection.aggregate([
{
$project: {
size: { $size: "$grouped_foos" },
foo_sum: {
$reduce: {
input: "$grouped_foos",
initialValue: [],
in: {
$map: {
input: { $range: [ 0, { $size: "$$this.foo" }, 1 ] },
as: "index",
in: {
$add: [
{ $arrayElemAt: [ "$$this.foo", "$$index" ] },
{ $ifNull: [ { $arrayElemAt: [ "$$value", "$$index" ] }, 0 ] }
]
}
}
}
}
}
}
},
{
$project: {
_id: 1,
avg_foo: {
$map: {
input: "$foo_sum",
in: {
$divide: [ "$$this", "$size" ]
}
}
}
}
}
])
推荐阅读
- c++ - 链接器错误取决于声明实例的位置
- javascript - 如何动态添加编辑按钮
- tensorflow - Tensorflow 多项式线性回归曲线拟合
- vba - 自动将已删除邮件中的电子邮件标记为已读 Outlook 2016 VBA
- azerothcore - 在哪里设置辛达苟萨的泰尔粉碎法术范围
- pyspark - AttributeError:“NoneType”对象在 model.surrogateDF 上没有属性“setCallSite”
- javascript - 在 Firefox 的页面中找到添加以下 html 的源
- java - 尝试按日期查询时如何使用 Spring JPA 进行自定义查询?
- c# - 从 CMD 控制台运行已发布的 Web (C# MVC)
- python - 使用 Plotly 绘制密度直方图