How to Use $lookup in MongoDB

MongoDB - $lookup Aggregation Pipeline Stage Definition The $lookup aggregation pipeline stage in MongoDB is used to perform a left outer join between two collections. It combines documents from one collection with matching documents from another collection based on specified fields. Steps to Use $lookup Aggregation Pipeline Stage 1). Open the MongoDB Shell using mongosh. 2). Select the database using use database_name. 3). Check the existing documents using find(). 4). Use the aggregate() method on the primary collection. 5). Add the $lookup stage to the aggregation pipeline. 6). Specify the collection to join using the from field. 7). Define the matching fields using localField and foreignField. 8). Specify the output array field using as. 9). Execute the aggregation query. 10). View the joined documents returned by MongoDB. Example use studentdb db.students.aggregate([ { $lookup: { from: "courses", localField: "course_id", foreignField: "course_id", as: "courseDetails" } } ]) Explanation: This query joins the students collection with the courses collection using the course_id field and stores the matching course information in the courseDetails array.