Sequence NODE_272
MediumJoin Posts with Author Details Using $lookup
MongoDB
Technical Specification
Use aggregation pipeline with $lookup to fetch posts along with author name and email from users.
Input/Output Samples
Input:posts + users
Output:{ title, body, author: { name, email } }
Optimal Logic Path
db.posts.aggregate([
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "author",
},
},
{ $unwind: "$author" },
{
$project: {
title: 1,
body: 1,
author: { name: "$author.name", email: "$author.email" },
},
},
]);Architectural Deep-Dive
$lookup allows SQL-like joins between collections so you can combine posts and authors in one result.