Database
[{"classes":[{"classId":"SSC","studentIds":["1"]},{"classId":"HSC","studentIds":["2","3"]}],"students":[{"_id":"1","student":{}},{"_id":"2","student":{}},{"_id":"3","student":{}}],}]
Query
db.collection.aggregate([{$project:{classes:{$map:{input:"$classes",as:"c",in:{classId:"$$c.classId",students:{$filter:{input:"$students",cond:{$in:["$$this._id","$$c.studentIds"]}}}}}}}}])