首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB Aggregation -嵌套数组的总和

MongoDB Aggregation -嵌套数组的总和
EN

Stack Overflow用户
提问于 2017-07-25 01:36:16
回答 1查看 5.1K关注 0票数 2

我正在尝试获取数组中特定字段的总和。问题是,该数组也在另一个数组中。我的文档如下(简化):

代码语言:javascript
复制
{
    "nome": "test-1"
    "notas": [{
        "numero_fiscal": "0001",
        "valores": {
            "portal": 1000,
            "arquivo": 1000
        },
        "abatimentos": [{
            "valor": 250,
            "tipo": "TIPO 1"
        }, {
            "valor": 250,
            "tipo": "TIPO 2"
        }]
    }, {
        "numero_fiscal": "0002",
        "valores": {
            "portal": 2000,
            "arquivo": 2000
        },
        "abatimentos": [{
            "valor": 500,
            "tipo": "TIPO 1"
        }, {
            "valor": 500,
            "tipo": "TIPO 2"
        }]
    }]
}

我希望我的输出是这样的:

代码语言:javascript
复制
{
    "_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"
}

我尝试了以下聚合(在其他聚合中,但结果总是相同的):

代码语言:javascript
复制
Notas.aggregate()
.match(my_query)
.unwind('notas') // unwinding 'notas' array
.group({
    '_id': 'resumo',
    'total_notas': { '$sum': 1 },
    'abatimentos': { '$push': '$notas.abatimentos' },
    'soma_portal': { '$sum': { '$notas.valores.portal' } },
    'soma_arquivo': { '$sum': { '$notas.valores.arquivo' } }
})
.unwind('$abatimentos')
.group({
    '_id': '$_id',
    'total_notas': { '$first': '$total_notas' },
    'soma_portal': { '$first': '$soma_portal' },
    'soma_arquivo': { '$first': '$soma_arquivo' },
    'soma_abatimento': { '$sum': '$abatimentos.valor' }
})

这几乎给了我想要的所有东西,但是'soma_abatimento‘总是0,而不是1500

我走的路对吗?或者这是不应该在数据库查询上执行的操作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-28 06:48:49

在您当前的版本中,您需要添加一个double数组,因为正如您已经正确识别的那样,在您的第一个$group之后,数组中有一个数组。

这里是一个稍微修改过的版本,它产生了预期的结果。

代码语言:javascript
复制
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

代码语言:javascript
复制
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" }
  }
}])
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45286724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档