Database
[{"category":"fruit","date":"2023-01-01","amount":1,},{"category":"fruit","date":"2023-01-02","amount":2,},{"category":"meat","date":"2023-01-03","amount":3,},{"category":"nuts","date":"2023-01-04","amount":4,},{"category":"nuts","date":"2023-01-05","amount":5,},{"category":"nuts","date":"2023-01-06","amount":6,}]
Query
db.collection.aggregate([{"$addFields":{"dateValue":{"$toDate":"$date"}}},{"$sort":{dateValue:1}},{"$group":{"_id":"$category","count":{"$sum":1},"lastAmount":{"$last":"$amount"}}}])