Je moet eerst $unwind
de questions
array en moet dan $lookup
. toepassen en gebruik ten slotte $group
om weer terug te gaan naar de array.
db.games.aggregate([
{ "$unwind": "$questions" },
{ "$lookup": {
"from": "questions",
"localField": "questions.question_id",
"foreignField": "_id",
"as": "question_data"
}},
{ "$unwind": "$question_data" },
{ "$addFields": {
"question_data.position": "$questions.position",
"question_data.question_id": "$questions.question_id"
}},
{ "$group": {
"_id": "$_id",
"questions": { "$push": "$questions" },
"question_data": { "$push": "$question_data" }
}}
])