收藏
回答

数据库中,怎么直接求子数据的和?

{"name":"A","pro":[{"name":"A1","total":1},{"name":"A2","total":2},{"name":"A3","total":3}......]}

{"name":"C","pro":[{"name":"C1","total":1},{"name":"C2","total":2},......]}

{"name":"B","pro":[{"name":"B1","total":1},{"name":"B2","total":2},{"name":"B3","total":3}......]}

上面的数据库中,怎么直接求所有total的和?不用分组。

db.collection('CARS').aggregate().group({
_id: null,
total: $.sum('$pro.total')
}).end()


这样测试不成功!

回答关注问题邀请回答
收藏

1 个回答

  • 老张
    老张
    2022-03-22

    unwind一下先,再group

    2022-03-22
    有用 2
    回复 1
    • 海纳百川
      海纳百川
      2022-03-22
      谢谢!
      2022-03-22
      回复
登录 后发表内容