Database
[{_id:ObjectId("669b5ec1ded042cdc2482f92"),Rollno:2,name:"anusha",subject:"OSD",marks:80},{_id:ObjectId("669b5ec1ded042cdc2482f93"),Rollno:3,name:"ravi",subject:"TOC",marks:69},{_id:ObjectId("669b5ec1ded042cdc2482f94"),Rollno:4,name:"veena",subject:"TOC",marks:70},{_id:ObjectId("669b5ec1ded042cdc2482f95"),Rollno:5,name:"Pravini",subject:"OSD",marks:80},{_id:ObjectId("669b5ec2ded042cdc2482f96"),Rollno:6,name:"Reena",subject:"DMSA",marks:50},{_id:ObjectId("669b5ec2ded042cdc2482f97"),Rollno:7,name:"Geeta",subject:"CN",marks:90},{_id:ObjectId("669b5eeaded042cdc2482f98"),Rollno:1,name:"Navin ",subject:"DMSA",marks:78}]
Query
db.collection.aggregate([{$group:{_id:"$subject",marks:{$max:"$marks"}}},{$lookup:{from:"collection",localField:"_id",foreignField:"subject",let:{highestMarks:"$marks"},pipeline:[{$match:{$expr:{$eq:["$$highestMarks","$marks"]}}},{$project:{name:1,Rollno:1}}],as:"matches"}}])