Database

[{"mmyy":"JAN-2019","location":"Boston","category":"Shoes","qty":5},{"mmyy":"JAN-2019","location":"Boston","category":"Shoes","qty":6},{"mmyy":"JAN-2019","location":"New York","category":"Shoes","qty":10},{"mmyy":"JAN-2019","location":"Boston","category":"Hats","qty":2},{"mmyy":"JAN-2019","location":"New York","category":"Hats","qty":3},{"mmyy":"FEB-2019","location":"Boston","category":"Shoes","qty":5},{"mmyy":"FEB-2019","location":"New York","category":"Hats","qty":10},]

Query

db.collection.aggregate([{"$group":{"_id":{"month":"$mmyy","location":"$location"},"total":{"$sum":"$qty"},"categories":{"$push":{"name":"$category","total":"$qty"}}}},{"$group":{"_id":"$_id.month","monthly_total":{"$sum":"$total"},"locations":{"$push":{"name":"$_id.location","categories":"$categories","location_total":{"$sum":"$categories.total"}}}}}])

Result