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","name":"hello 4",postId:"5",likes:[1,2,3,4,5,6,7,8]},{"_id":"c3","name":"hello five",postId:"4",likes:[1,3]}],"replies":[{"_id":"r2","body":"hello",commentId:"c1",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:"comments.replies",},},])

Result