Real-world reporting requires combining documents from disparate collections and manipulating nested arrays. The $lookup stage performs true server-side left outer joins between collections, and the $unwind stage deconstructs array fields into individual stream documents.
$lookup StageThe $lookup stage performs a left outer join to an unsharded collection in the same database:
Note: $lookup always outputs an array in the as field, even if only one matching document is found.
$unwind StageArrays often contain multiple elements that need to be analyzed individually. The $unwind stage deconstructs an array field from the input documents to output a document for each element in the array:
$lookup, $unwind, and $groupFind the most popular course tags across all enrollments:
$lookup aggregation stage perform?$lookup stage assign to the output field designated by as?$unwind stage do when applied to an array with 4 elements?$unwind if a document has an empty array [] or the field is missing/null?foreignField in a joined $lookup collection be indexed?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.