Database

[{username:"jack",events:[{eventType:"party",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"87654321-0ebb-4238-8bf7-87654321"}},{eventType:"party",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"87654321-0ebb-4238-8bf7-87654321"}},{eventType:"party",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"01234567-0ebb-4238-8bf7-01234567"}},{eventType:"party",createdAt:ISODate("2022-01-30T10:26:11.214Z"),visitorInfo:{visitorId:"12345678-0ebb-4238-8bf7-12345678"}},{eventType:"party",createdAt:ISODate("2022-01-30T10:16:11.214Z"),visitorInfo:{visitorId:"12345678-0ebb-4238-8bf7-12345678"}}]},{username:"jack",events:[{eventType:"meeting",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"87654321-0ebb-4238-8bf7-87654321"}},{eventType:"meeting",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"87654321-0ebb-4238-8bf7-87654321"}},{eventType:"meeting",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"01234567-0ebb-4238-8bf7-01234567"}},{eventType:"meeting",createdAt:ISODate("2022-01-30T10:26:11.214Z"),visitorInfo:{visitorId:"12345678-0ebb-4238-8bf7-12345678"}},{eventType:"meeting",createdAt:ISODate("2022-01-30T10:16:11.214Z"),visitorInfo:{visitorId:"12345678-0ebb-4238-8bf7-12345678"}}]},{username:"jack",events:[{eventType:"test",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"87654321-0ebb-4238-8bf7-87654321"}},{eventType:"test",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"87654321-0ebb-4238-8bf7-87654321"}},{eventType:"test",createdAt:ISODate("2022-01-23T10:26:11.214Z"),visitorInfo:{visitorId:"01234567-0ebb-4238-8bf7-01234567"}},{eventType:"test",createdAt:ISODate("2022-01-30T10:26:11.214Z"),visitorInfo:{visitorId:"12345678-0ebb-4238-8bf7-12345678"}},{eventType:"test",createdAt:ISODate("2022-01-30T10:16:11.214Z"),visitorInfo:{visitorId:"12345678-0ebb-4238-8bf7-12345678"}}]}]

Query

db.collection.aggregate([{$match:{username:"jack"}},{"$unwind":"$events"},{"$match":{"events.eventType":{"$in":["meeting","party"]}}},{"$group":{"_id":{date:{"$dateToString":{format:"%Y-%m-%d",date:"$events.createdAt"}},"visitorId":"$events.visitorInfo.visitorId","eventType":"$events.eventType"},"count":{"$sum":1}}},{"$group":{"_id":{"date":"$_id.date","eventType":"$_id.eventType"},"uniqueTotal":{"$sum":1},total:{"$sum":"$count"}}},{"$group":{"_id":"$_id.date","partyUniqueTotal":{"$sum":{"$cond":[{$eq:["$_id.eventType","party"],},"$uniqueTotal",0]}},"totalPartyEvents":{"$sum":{"$cond":[{$eq:["$_id.eventType","party"],},"$total",0]}},"meetingUniqueTotal":{"$sum":{"$cond":[{$eq:["$_id.eventType","meeting"],},"$uniqueTotal",0]}},"totalmeetingEvents":{"$sum":{"$cond":[{$eq:["$_id.eventType","meeting"],},"$total",0]}}}}])

Result