Database

[{"collection":"users","count":5,"content":{"_id":{"type":"autoincrement","autoType":"int"},"lastLocationId":{"type":"constant","constVal":{"$oid":"0123456789abcdef01234567"}},"isActive":{"type":"boolean"},"workerId":{"type":"stringFromParts","parts":[{"type":"autoincrement","autoType":"int"}]},"workerFirstName":{"type":"faker","method":"FirstName"},"workerSurname":{"type":"faker","method":"LastName"}}},{"collection":"attendances","count":100,"content":{"employeeId":{"type":"enum","values":[0,1,2,3,4]},"workerId":{"type":"stringFromParts","parts":[{"type":"enum","values":[0,1,2,3,4]}]},"Date":{"type":"date","startDate":"2022-08-25T00:00:00Z","endDate":"2022-11-05T23:59:59Z"},"createdAs":{"type":"enum","randomOrder":true,"values":["ABSENT","PRESENT","PRESENT","PRESENT","PRESENT"]},"status":{"type":"boolean"},"workerType":{"type":"enum","randomOrder":true,"values":["Employee","Employee","Employee","Employee","Employee","Contractor","Intern"]}}}]

Query

db.users.aggregate([{"$match":{"lastLocationId":ObjectId("0123456789abcdef01234567"),"isActive":true}},{"$project":{"workerId":1,"workerFirstName":1,"workerSurname":1}},{"$lookup":{"from":"attendances","localField":"_id","foreignField":"employeeId","as":"attendances",/** do all the matching here*/"pipeline":[{"$match":{"Date":{/** fromDate, toDate*/"$gte":ISODate("2022-09-01T00:00:00Z"),"$lte":ISODate("2022-09-30T23:59:59Z")},/** dataType*/"createdAs":"ABSENT","status":true,/** workerType*/"workerType":"Employee"}},{"$project":{"Date":1,"createdAs":1}}]}},{$skip:0},{$limit:10}])

Result