Database
[{"_id":"622993ef8b87bc000184114a","currencyCode":"USD","businessDayStartDate":"2022-03-05","hourZoneNumber":1,"customerCount":0,"itemQuantity":4,"nodeId":"STORE_1","endpointId":"998","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"4"}},{"_id":"622993ef8b87bc000184114b","currencyCode":"USD","businessDayStartDate":"2022-03-06","hourZoneNumber":2,"customerCount":2,"itemQuantity":4,"nodeId":"STORE_1","endpointId":"999","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"4"}},{"_id":"622993ef8b87bc000184114c","currencyCode":"USD","businessDayStartDate":"2022-03-04","hourZoneNumber":1,"customerCount":4,"itemQuantity":0,"nodeId":"STORE_DEV_2","endpointId":"999","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"41.86"}},{"_id":"622993ef8b87bc000184114d","currencyCode":"USD","businessDayStartDate":"2022-03-04","hourZoneNumber":1,"customerCount":0,"itemQuantity":0,"nodeId":"STORE_DEV_2","endpointId":"999","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"4"}},{"_id":"622993ef8b87bc000184114e","currencyCode":"USD","businessDayStartDate":"2022-03-04","hourZoneNumber":2,"customerCount":0,"itemQuantity":0,"nodeId":"STORE_DEV","endpointId":"998","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"38.06"}},{"_id":"622993ef8b87bc000184114f","currencyCode":"USD","businessDayStartDate":"2022-03-04","hourZoneNumber":1,"customerCount":0,"itemQuantity":0,"nodeId":"STORE_DEV","endpointId":"998","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"41.86"}},{"_id":"622993ef8b87bc0001841150","currencyCode":"USD","businessDayStartDate":"2022-03-04","hourZoneNumber":1,"customerCount":0,"itemQuantity":0,"nodeId":"STORE_DEV","endpointId":"999","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"41.86"}},{"_id":"622993ef8b87bc0001841151","currencyCode":"USD","businessDayStartDate":"2022-03-04","hourZoneNumber":1,"customerCount":0,"itemQuantity":0,"nodeId":"STORE_DEV","endpointId":"999","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"38.06"}},{"_id":"622993ef8b87bc0001841152","currencyCode":"USD","businessDayStartDate":"2022-03-04","hourZoneNumber":1,"customerCount":0,"itemQuantity":8,"nodeId":"STORE_DEV","endpointId":"999","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"38.06"}},{"_id":"622993ef8b87bc0001841153","currencyCode":"USD","businessDayStartDate":"2022-03-04","hourZoneNumber":1,"customerCount":0,"itemQuantity":0,"nodeId":"STORE_DEV","endpointId":"999","businessDayId":"377a433f6a73e31aef39e975831a9f8c","amount":{"$numberDecimal":"3.8"}},{"_id":"622b39cf8b87bc0001841885","currencyCode":"USD","businessDayStartDate":"2022-03-10","customerCount":0,"itemQuantity":0,"nodeId":"STORE","endpointId":"999","businessDayId":"fdfccaace2a9e745cb3eab50dc811076","amount":{"$numberDecimal":"0"}},{"_id":"622b39cf8b87bc0001841886","currencyCode":"USD","businessDayStartDate":"2022-03-10","customerCount":0,"itemQuantity":0,"nodeId":"STORE_DEV","endpointId":"999","businessDayId":"fdfccaace2a9e745cb3eab50dc811076","amount":{"$numberDecimal":"200"}},{"_id":"622b39cf8b87bc0001841887","currencyCode":"USD","businessDayStartDate":"2022-03-10","customerCount":0,"itemQuantity":0,"nodeId":"STORE_DEV","endpointId":"999","businessDayId":"fdfccaace2a9e745cb3eab50dc811076","amount":{"$numberDecimal":"200"}}]
Query
db.collection.aggregate([{"$match":{"nodeId":{"$in":["STORE_DEV_2","STORE_DEV"]},"businessDayStartDate":{"$gte":"2022-03-04","$lte":"2022-03-07"}}},{"$sort":{"nodeId":1,"endpointId":1,"hourZoneNumber":1}},{"$group":{"_id":{"nodeId":"$nodeId","endpointId":"$endpointId","hourZoneNumber":"$hourZoneNumber"},"customerCount":{"$sum":"$customerCount"},"itemQuantity":{"$sum":"$itemQuantity"},"amount":{"$sum":"$amount"}}},{"$group":{"_id":{"nodeId":"$_id.nodeId","endpointId":"$_id.endpointId"},"hourZones":{"$addToSet":{"hourZoneNumber":"$_id.hourZoneNumber","customerCount":{"$sum":"$customerCount"},"itemQuantity":{"$sum":"$itemQuantity"},"amount":{"$sum":"$amount"}}}}},{"$group":{"_id":"$_id.nodeId","endpoints":{"$addToSet":{"endpointId":"$_id.endpointId","hourZones":"$hourZones"}}}},{$project:{_id:0,nodeId:"$_id",endpoints:1,hourZones:1,total:1}}])