Musisz $unwind
spłaszczyć zarówno skalarne, jak i podrzędne obce _ids i dodać $group
etap na końcu potoku, aby odzyskać oryginalną strukturę.
$first
akumulator do przechowywania pól i $push
z $arrayElemAt
aby zgromadzić wartości tablicy w celu dostosowania dla $unwind
var linkedModels = [
{
"$match": { "del": false }
},
{
"$lookup": {
from: "charnpcalign",
localField: "align",
foreignField: "_id",
as: "linked_align"
}
},
{
"$unwind":"$classes"
},
{
"$lookup": {
from: "charnpcclass",
localField: "classes",
foreignField: "_id",
as: "linked_classes"
}
},
{
"$group": {
"_id": "$_id",
"name": {"$first":"$name"},
"align": {"$first":"$align"},
"classes":{"$push":"$classes"},
"carrying":{"$first":"$carrying"},
"linked_align":{"$first":"$linked_align"},
"linked_classes":{"$push":{"$arrayElemAt":["$linked_classes",0]}}
}
},
{
"$unwind":"$carrying"
},
{
"$lookup": {
from: "item",
localField: "carrying.itemId",
foreignField: "_id",
as: "linked_carrying"
}
},
{
"$group": {
"_id": "$_id",
"name": {"$first":"$name"},
"align": {"$first":"$align"},
"classes":{"$first":"$classes"},
"linked_align":{"$first":"$linked_align"},
"carrying":{"$push":"$carrying"},
"linked_carrying":{"$push":{"$arrayElemAt":["$linked_carrying",0]}}
}
}
]
Nie potrzebujesz $unwind
na tablicy skalarnej (classes
) w wersji 3.4 i możesz zastąpić {"classes":{"$push":"$classes"}}
&{"linked_classes":{"$push":{$arrayElemAt:["$linked_classes",0]}}}
z {"classes":{"$first":"$classes"}}
&{"linked_classes":{"$first":"$linked_classes"}}
odpowiednio.