Database

[{"amount":{"$numberDecimal":"1"},"appId":3,"createdDttm":{"$date":"2023-09-15T10:17:41.350Z"},"day":15,"hour":10,"month":9,"newUser":true,"userId":"11111","year":2023},{"amount":{"$numberDecimal":"10"},"appId":3,"createdDttm":{"$date":"2023-09-15T16:17:41.350Z"},"day":15,"hour":16,"month":9,"newUser":true,"userId":"11111","year":2023},{"amount":{"$numberDecimal":"100"},"appId":7,"createdDttm":{"$date":"2023-09-16T16:00:01.135Z"},"day":16,"hour":16,"month":9,"userId":"11111","year":2023},{"amount":{"$numberDecimal":"2"},"appId":5,"createdDttm":{"$date":"2023-09-15T16:00:41.100Z"},"day":15,"hour":16,"month":9,"newUser":true,"userId":"22222","year":2023}]

Query

db.collection.aggregate([{$match:{year:2023,month:9,day:15,newUser:true}},{$group:{_id:null,distinctUserId:{$addToSet:"$userId"}}},{$lookup:{from:"collection",localField:"distinctUserId",foreignField:"userId",pipeline:[{$project:{_id:0,appId:1,amount:1}}],as:"result"}},{$unset:["_id","distinctUserId"]},{$unwind:{path:"$result",preserveNullAndEmptyArrays:false}},{$project:{_id:0,appId:"$result.appId",amount:"$result.amount"}},{$group:{_id:{appId:"$appId"},paidNewUserIncome:{$sum:"$amount"}}}])

Result