如何汇总所有文档的关键值

时间:2019-01-22 09:40:36

标签: javascript node.js mongodb

我刚接触Mongo DB时,在汇总所有文档的键值时面临着一个问题...

这里我提供了Mongo DB的样本数据-

{ _id: 1, cust_id: "abc1", ord_date: ISODate("2012-11-02T17:04:11.102Z"), status: "A", amount: 50 }    

{ _id: 2, cust_id: "xyz1", ord_date: ISODate("2013-10-01T17:04:11.102Z"), status: "A", amount: 100 }    

{ _id: 3, cust_id: "xyz1", ord_date: ISODate("2013-10-12T17:04:11.102Z"), status: "D", amount: 25 }    

{ _id: 4, cust_id: "xyz1", ord_date: ISODate("2013-10-11T17:04:11.102Z"), status: "D", amount: 125 }    

{ _id: 5, cust_id: "abc1", ord_date: ISODate("2013-11-12T17:04:11.102Z"), status: "A", amount: 25 }

我只想获取键“金额”的总值,例如- 50+100+25+125+25

1 个答案:

答案 0 :(得分:1)

您可以这样查询:

<svg width="5cm" height="4cm" version="1.1"
     xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink">
    <image xlink:href="https://www.gettyimages.com/gi- 
       resources/images/CreativeLandingPage/HP_Sept_24_2018/CR3_GettyImages- 
      159018836.jpg" x="0" y="0" height="50px" width="50px"/>
</svg>

有关聚合的更多详细信息,请访问here,有关 sum 的更多详细信息,请here