Database

db={"document":[{"_id":ObjectId("5eb256c9e519051af2eeb5f7"),"name":"Income","types":[{"typeId":ObjectId("5eb257a3e519051af2eeb624"),"name":"W2","tenantId":null,"message":"","createdOn":null,"createdBy":1.0,"isActive":true},{"typeId":ObjectId("5eb257a3e519051af2eeb639"),"name":"Salary Slip","tenantId":1,"message":"","createdOn":null,"createdBy":1.0,"isActive":true}]}],"request":[{"_id":ObjectId("5eb25d1fe519051af2eeb72d"),"employeeId":1234,"customerId":1275,"tenantId":1,"createdOn":ISODate("2013-10-01T00:00:00.000Z"),"loanApplicationId":1.0,"status":"requested","message":"Dear John, please send following documents.","documents":[{"typeId":null,"displayName":"W2 2016","message":"please upload salary slip for year 2016","status":"requested","files":[]},{"typeId":ObjectId("5eb257a3e519051af2eeb624"),"displayName":"W2 2016","message":"please upload salary slip for year 2016","status":"requested","files":[]}]}]}

Query

db.request.aggregate([{$unwind:"$documents"},{$lookup:{from:"document",let:{req_typeId:"$documents.typeId"},pipeline:[{$unwind:"$types"},{$match:{$expr:{$eq:["$$req_typeId","$types.typeId"]}}}],as:"documents.example"}},{$unwind:{path:"$documents.example",preserveNullAndEmptyArrays:true}},{$group:{_id:"$_id",employeeId:{$first:"$employeeId"},customerId:{$first:"$customerId"},tenantId:{$first:"$tenantId"},createdOn:{$first:"$createdOn"},loanApplicationId:{$first:"$loanApplicationId"},status:{$first:"$status"},message:{$first:"$message"},documents:{$push:{typeId:"$documents.typeId",typeInfo:{$cond:[{$eq:["$documents.example",undefined]},null,{name:"$documents.example.types.name"}]},displayName:"$documents.displayName",message:"$documents.message",status:"$documents.status",files:"$documents.files"}}}}])

Result