A social media platform stores posts in a collection named posts. Each post document contains an array field `comments`, where each comment is a subdocument with fields `user` and `text`. The development team needs to generate a report that lists each comment as a separate document, including the post's `_id` and the comment's `user` and `text`. Which aggregation stage should be used to achieve this?
The $unwind stage deconstructs an array field from the input documents to output a document for each element. In this scenario, using $unwind on the `comments` array will produce one document per comment, preserving the parent post's fields such as `_id`. This exactly matches the requirement to list each comment as a separate document. Additional stages like $project can then reshape the output to include only the desired fields.
Why this answer
The $unwind stage is specifically designed to deconstruct an array field, outputting one document per array element. In this case, unwinding the `comments` array will produce a document for each comment, retaining the parent post's `_id`. This is the correct stage to flatten the comments array and meet the reporting requirement.
Exam trap
The trap here is confusing $project with $unwind; $project can include the array but does not create separate documents for each element.