How to calculate the sales volume and the price of the latest sale by grouping the sales records by commodity?

  mongodb, question
Db.sales.insert (""merchandise ":"jacket ","sales time ":"2015-12-01 11:00 ","selling price ":1.3});
 Db.sales.insert (""merchandise ":"jacket ","sales time ":"2015-12-01 12:00 ","selling price ":1.4});
 Db.sales.insert (""goods ":"trousers ","sales time ":"2015-12-01 11:30 ","selling price ":2.3});
 Db.sales.insert (""goods ":"trousers ","sales time ":"2015-12-01 12:30 ","selling price ":2.4});
 Db.sales.insert (""merchandise ":"shoes ","sales time ":"2015-12-01 11:00 ","selling price ":3.3});
 Db.sales.insert (""merchandise ":"shoes ","sales time ":"2015-12-01 12:00 ","selling price ":3.4});
 Db.sales.insert (""merchandise ":"shoes ","sales time ":"2015-12-01 12:30 ","selling price ":3.5});
db.sales.aggregate([
 { $group: {
 _id: "$ commodity ",
 "sales": {$sum:1},
 "Last Sales Time": {$max: "$ Sales Time "}
 } }
 ]);

Question: How to find the selling price corresponding to the last selling time?

图片描述

Hope this helps.

Just use forEach in the case, and hope this helps.

Love MongoDB! Have Fun!