2017-03-22 38 views
0

在與組所需的輸出MongoDB的排序查詢是具有記錄等我的MongoDB集合...如何得到由

{"type":"ORDER_PLACE", "price":100, "emailId":"[email protected]"} 
{"type":"CART_ORDER", "price":150, "emailId":"[email protected]"} 
{"type":"ORDER_PLACE", "price":200, "emailId":"[email protected]"} 
{"type":"CART_ORDER", "price":350, "emailId":"[email protected]"} 
{"type":"CART_ORDER", "price":250, "emailId":"[email protected]"} 
{"type":"ORDER_PLACE", "price":240, "emailId":"[email protected]"} 
{"type":"ORDER_PLACE", "price":330, "emailId":"[email protected]"} 

我想要的結果應該是這樣......

{"emailId":"[email protected]", "type":"ORDER_PLACE", "total":540} 
{"emailId":"[email protected]", "type":"ORDER_PLACE", "total":330} 

什麼將是排序查詢來獲得上面的結果(desc order)。

回答

0

這裏是查詢,讓您導致您的結構:

db.coll.aggregate([{$match: {type: "ORDER_PLACE"}}, {$group: {_id: "$emailId", total: {$sum: "$price"}}},{$project:{_id:0,emailId:"$_id",type:"ORDER_PLACE",total:"$total"}}])