Arrays MongoDB聚合-嵌套数组的和值
我试图得到数组中特定字段的和。问题是,该数组也在另一个数组中。我的文件如下(简化): 我希望我的输出类似于:Arrays MongoDB聚合-嵌套数组的和值,arrays,mongodb,aggregation-framework,Arrays,Mongodb,Aggregation Framework,我试图得到数组中特定字段的和。问题是,该数组也在另一个数组中。我的文件如下(简化): 我希望我的输出类似于: { "_id": "resumo", "total_notas": 2, // this is the counter for the array "notas" "soma_portal": 3000, // this is the sum of the "valores.portal" of all "notas" "soma_arquivo": 30
{
"_id": "resumo",
"total_notas": 2, // this is the counter for the array "notas"
"soma_portal": 3000, // this is the sum of the "valores.portal" of all "notas"
"soma_arquivo": 3000, // this is the sum of the "valores.arquivo" of all "notas"
"soma_abatimento": 1500 // this is the sum of all the "abatimentos.valor" from all the "notas"
}
我尝试了以下聚合(其中包括其他聚合,但结果始终相同):
这给了我几乎所有我想要的东西,但是'soma_abatimento'总是0,而不是1500
我走对了吗?或者这是不应该在数据库查询中执行的操作?在当前版本中,您需要添加一个双$unwind,因为正如您正确识别的那样,在第一个$group之后,您在一个数组中有一个数组 下面是一个稍微修改过的版本,它产生了预期的结果
db.Notas.aggregate([{
$unwind: "$notas"
}, {
$group: {
_id: "resumo",
total_notas: { $sum: 1 },
soma_portal: { $sum: "$notas.valores.portal" },
soma_arquivo: { $sum: "$notas.valores.arquivo" },
abatimentos: { $push: "$notas.abatimentos" }
}
}, {
$unwind: "$abatimentos"
}, {
$unwind: "$abatimentos"
}, {
$group: {
_id: "$_id",
total_notas: { $first: "$total_notas" },
soma_portal: { $first: "$soma_portal" },
soma_arquivo: { $first: "$soma_arquivo" },
soma_abatimentos: { $sum: "$abatimentos.valor" }
}
}])
如果您能够使用MongoDB>=3.4,我建议使用一种不同的方法来减少文档大小,并避免使用MongoDB 3.4中添加的$reduce进行多个$unwind
db.Notas.aggregate([{
$unwind: "$notas"
}, {
$project: {
_id: 0,
portal: "$notas.valores.portal",
arquivo: "$notas.valores.arquivo",
abatimentos: {
$reduce: {
input: "$notas.abatimentos",
initialValue: 0,
in: { $add: ["$$value", "$$this.valor"] }
}
}
}
}, {
$group: {
_id: "resumo",
total_notas: { $sum: 1 },
soma_portal: { $sum: "$portal" },
soma_arquivo: { $sum: "$arquivo" },
soma_abatimentos: { $sum: "$abatimentos" }
}
}])
在当前版本中,您需要添加一个双$unwind,因为正如您正确识别的那样,在第一个$group之后,您在一个数组中有一个数组 下面是一个稍微修改过的版本,它产生了预期的结果
db.Notas.aggregate([{
$unwind: "$notas"
}, {
$group: {
_id: "resumo",
total_notas: { $sum: 1 },
soma_portal: { $sum: "$notas.valores.portal" },
soma_arquivo: { $sum: "$notas.valores.arquivo" },
abatimentos: { $push: "$notas.abatimentos" }
}
}, {
$unwind: "$abatimentos"
}, {
$unwind: "$abatimentos"
}, {
$group: {
_id: "$_id",
total_notas: { $first: "$total_notas" },
soma_portal: { $first: "$soma_portal" },
soma_arquivo: { $first: "$soma_arquivo" },
soma_abatimentos: { $sum: "$abatimentos.valor" }
}
}])
如果您能够使用MongoDB>=3.4,我建议使用一种不同的方法来减少文档大小,并避免使用MongoDB 3.4中添加的$reduce进行多个$unwind
db.Notas.aggregate([{
$unwind: "$notas"
}, {
$project: {
_id: 0,
portal: "$notas.valores.portal",
arquivo: "$notas.valores.arquivo",
abatimentos: {
$reduce: {
input: "$notas.abatimentos",
initialValue: 0,
in: { $add: ["$$value", "$$this.valor"] }
}
}
}
}, {
$group: {
_id: "resumo",
total_notas: { $sum: 1 },
soma_portal: { $sum: "$portal" },
soma_arquivo: { $sum: "$arquivo" },
soma_abatimentos: { $sum: "$abatimentos" }
}
}])
谢谢tkbyte,它工作得很好!MongoDB>=3.4更好,所以感谢您提供了这两种方式。感谢tkbyte,它工作得非常好!MongoDB>=3.4更好,因此感谢您提供这两种方法。