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"}},{$project:{_id:1,employeeId:1,customerId:1,tenantId:1,createdOn:1,loanApplicationId:1,status:1,message:1,documents:1,typeInfo:{$cond:[{$eq:[{$size:"$documents.example"},0]},null,"$documents.example.types.name"]}}},{$group:{_id:{_id:"$_id",employeeId:"$employeeId",customerId:"$customerId",tenantId:"$tenantId",createdOn:"$createdOn",loanApplicationId:"$loanApplicationId",status:"$status",message:"$message",documents:"$documents",typeInfo:"$typeInfo"}}},{$project:{_id:"$_id._id",employeeId:"$_id.employeeId",customerId:"$_id.customerId",tenantId:"$_id.tenantId",createdOn:"$_id.createdOn",loanApplicationId:"$_id.loanApplicationId",status:"$_id.status",message:"$_id.message",documents:"$_id.documents",typeInfo:"$_id.typeInfo"}},{$unwind:{path:"$documents.example",preserveNullAndEmptyArrays:true}},{$unwind:{path:"$typeInfo",preserveNullAndEmptyArrays:true}},{$project:{_id:1,employeeId:1,customerId:1,tenantId:1,createdOn:1,loanApplicationId:1,status:1,message:1,documents:{typeId:"$documents.typeId",typeInfo:{$cond:[{$eq:["$typeInfo",null]},null,{"name":"$typeInfo"}]},displayName:1,message:1,status:1,files:1}}},{$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:"$documents"}}}])

Result