时间:2021-07-01 10:21:17 帮助过:21人阅读
db.orders.aggregate([ { $group:{ _id:null, count:{$sum:1} } } ])
例2:
SQL:
SELECT SUM(price) AS total FROM orders
Mongodb:
db.orders.aggregate( [ { $group: { _id:null, total:{$sum:"$price"} } } ])
例3:
SQL:
SELECT cust_id,SUM(price) AS total FROM orders GROUP BY cust_id
Mongodb:
db.orders.aggregate([ { $group: { _id:"$cust_id", total: { $sum:"$price" } } }, { $sort: { total:1 } } ])
例4:
SQL:
SELECT cust_id, ord_date,SUM(price) AS total FROM orders GROUP BY cust_id, ord_date
Mongodb:
db.orders.aggregate([ { $group: { _id: { cust_id:"$cust_id", ord_date: { month:{$month:"$ord_date"}, day:{$dayOfMonth:"$ord_date"}, year:{$year:"$ord_date"} } }, total:{$sum:"$price"} } } ])
(译文)SQL与Mongodb聚合之前的对应关系
标签:match status price 参考 log group by 例子 聚合函数 ref