Database

db={"facebookPages":[{"_id":1},{"_id":2}],"channels":[{"_id":3},{"_id":4}],"channelPosts":[{"_id":5},{"_id":6}],"posts":[{"_id":7},{"_id":8}],"comments":[{"_id":9},{"_id":10}]}

Query

db.facebookPages.aggregate([/** Stage 1: Match the specific Facebook page by _id*/{$match:{_id:1}},/** Stage 2: Lookup the channel details for the page*/{$lookup:{from:"channels",localField:"channel",foreignField:"_id",as:"channel"}},/** Stage 3: Unwind the channel array to destructure the channel details*/{$unwind:"$channel"},/** Stage 4: Lookup channel posts related to the Facebook page*/{$lookup:{from:"channelposts",localField:"_id",foreignField:"fbPage",as:"channelPosts"}},/** Stage 5: Unwind the channelPosts array to destructure individual channel posts*/{$unwind:"$channelPosts"},/** Stage 6: Lookup posts linked to each channel post*/{$lookup:{from:"posts",localField:"channelPosts.post",foreignField:"_id",as:"posts"}},/** Stage 7: Unwind the posts array to destructure individual post details*/{$unwind:"$posts"},/** Stage 8: Add fields from channelPosts to each post*/{$addFields:{"posts.likes":"$channelPosts.likes","posts.comments":"$channelPosts.comments","posts.shares":"$channelPosts.shares","posts.publishDate":"$channelPosts.publishDate","posts.status":"$channelPosts.status","posts.channelPostId":"$channelPosts._id",/** Add channelPost ID to post*/}},/** Stage 9: Group the results by Facebook page _id*/{$group:{_id:"$_id",posts:{$push:"$posts"},/** Collect all posts into an array*/channelPosts:{$push:"$channelPosts"},/** Collect all channelPosts into an array*/channel:{$first:"$channel"},/** Keep the first channel details*/page:{$first:"$$ROOT"},/** Keep the entire original document (page)*/}},/** Stage 10: Lookup comments linked to each channel post*/{$lookup:{from:"comments",localField:"channelPosts._id",foreignField:"channelPost",as:"pageComments"}},/** Stage 11: Unwind the pageComments array to destructure individual comment details*/{$unwind:"$pageComments"},/** Stage 12: Add postContent field to each comment, mapping it to the corresponding post*/{$addFields:{"pageComments.postContent":{$arrayElemAt:[{$filter:{input:"$posts",as:"post",cond:{$eq:["$$post.channelPostId","$pageComments.channelPost"]}}},0]}}},/** Stage 13: Group again by Facebook page _id to format the output*/{$group:{_id:"$_id",posts:{$first:"$posts"},/** Keep the first post array*/channel:{$first:"$channel"},/** Keep the first channel details*/page:{$first:"$page"},/** Keep the first page details*/pageComments:{$push:"$pageComments"},/** Collect all comments into an array*/}},/** Stage 14: Project the final shape of the documents for the output*/{$project:{_id:1,/** Include _id field*/channel:{/** Structure the channel details*/_id:"$channel._id",channelUsername:"$channel.channelUsername",channelUserId:"$channel.channelUserId"},pageComments:{/** Structure the comments*/_id:1,commenter:1,commenterId:1,commentId:1,content:1,postContent:"$pageComments.postContent.message",/** Access the message field from postContent*/},posts:1,/** Include posts array*/page:{/** Structure the page details*/pageId:"$page.pageId",name:"$page.name",category:"$page.category",likesCount:"$page.likesCount",postsCount:"$page.postsCount",pictureUrl:"$page.pictureUrl",weeklyInsights:"$page.weeklyInsights",monthlyInsights:"$page.monthlyInsights",monthlyPostsInsights:"$page.monthlyPostsInsights"}}}])

Result