1. 程式人生 > >MongoDB 實現按列累加統計

MongoDB 實現按列累加統計

Mongodb 如何計算多條資料的總和

MySQL:

select date, sum(total) from test_table where date>="2018-02-26 00:00:00" and date<'2018-02-27 00:00:00' and my_id = 0;

MongoDB 聚合查詢:

db.getCollection('test_table').aggregate( [ { $match: { "my_id":0, "now_date":{"$gte":ISODate("2018-02-26T00:00:00.000Z"), "$lt": ISODate("2018-02-27T00:00:00.000Z")} } }, { $group
: { _id: null, total: { $sum: "$total" } } } ] )