以下是我的mongodb系列,其中包含......
等记录{"type":"ORDER_PLACE", "price":100, "emailId":"abcd1@gmail.com"}
{"type":"CART_ORDER", "price":150, "emailId":"abcd2@gmail.com"}
{"type":"ORDER_PLACE", "price":200, "emailId":"abcd1@gmail.com"}
{"type":"CART_ORDER", "price":350, "emailId":"abcd1@gmail.com"}
{"type":"CART_ORDER", "price":250, "emailId":"abcd2@gmail.com"}
{"type":"ORDER_PLACE", "price":240, "emailId":"abcd1@gmail.com"}
{"type":"ORDER_PLACE", "price":330, "emailId":"abcd2@gmail.com"}
我希望结果应该像......
{"emailId":"abcd1@gmail.com", "type":"ORDER_PLACE", "total":540}
{"emailId":"abcd2@gmail.com", "type":"ORDER_PLACE", "total":330}
获取上述结果的排序查询(desc order)。
答案 0 :(得分:0)
以下是为您提供结构结果的查询:
db.coll.aggregate([{$match: {type: "ORDER_PLACE"}}, {$group: {_id: "$emailId", total: {$sum: "$price"}}},{$project:{_id:0,emailId:"$_id",type:"ORDER_PLACE",total:"$total"}}])