Database

db={"posts":[{"_id":"2","body":"hello",likes:[]},{"_id":"5","body":"hello 2",likes:[1,2,]},{"_id":"4","body":"hello 3",likes:[1,7,8]},{"_id":"1","name":"hello 4",likes:[1,2,5,7,8]},{"_id":"3","name":"hello five",likes:[1,2,3,4,5,6,7,8]}],"comments":[{"_id":"c2","body":"hello",postId:"1",likes:[34,45,67]},{"_id":"c5","body":"hello 2",postId:"2",likes:[34,45,67]},{"_id":"c4","body":"hello 3",postId:"3",likes:[34,45,67]},{"_id":"c1","body":"hello 4",postId:"5",likes:[1,2,3,4,5,6,7,8]},{"_id":"c3","body":"hello five",postId:"4",likes:[1,3]}],"replies":[{"_id":"r2","body":"hello",commentId:"c1",likes:[1,2,3]},{"_id":"r9","body":"hello",commentId:"c3",likes:[1,2,3]},{"_id":"r5","body":"hello 2",commentId:"c2",likes:[1,2,3]},{"_id":"r4","body":"hello 3",commentId:"c3",likes:[1,2,3,4,5]},{"_id":"r1","name":"hello 4",commentId:"c5",likes:[1,2,3,4,5,5]},{"_id":"r3","name":"hello five",commentId:"c4",likes:[1,2,3,4,4,5,5]}]}

Query

db.posts.aggregate([{$lookup:{from:"comments",localField:"_id",foreignField:"postId",as:"comments",},},{$unwind:"$comments",},{$lookup:{from:"replies",localField:"comments._id",foreignField:"commentId",as:"replies",},},{$unwind:"$replies",},{"$addFields":{"replies.countOflikes":{$size:{$ifNull:["$replies.likes",[]]}},"replies.isLiked":{$cond:{if:{$eq:[{$size:{$filter:{input:"$replies.likes",as:"item",cond:{$eq:["$$item",1/**id of the user whom you wanna check if liked the reply*/]}}}},0]},then:false,else:true}}}},{$group:{_id:"$comments._id",postId:{$first:"$_id"},body:{$first:"$body"},"comments":{$first:"$comments"},replies:{$push:"$replies"}}},{$addFields:{"comments.replies":"$replies"}},{$group:{_id:"$postId",body:{$first:"$body"},comments:{$push:"$comments"}}}])

Result