$unwind after $lookup
Because $lookup always hands back an array, the stage that follows it is almost always $unwind. This pairing is common enough to be treated as a single idiom.
Classic pattern:
db.orders.aggregate([
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "user"
}
},
{
$unwind: "$user"
}
])
Now:
{
_id: 1,
userId: 101,
amount: 1000,
user: {
_id: 101,
name: "Denish"
}
}
This is the common pattern:
$lookup
↓
Returns array
$unwind
↓
Turns array into object
For one-to-one or many-to-one relationships, this is very common.