$lookup
avec user
collecte
$unwind
déconstruire id_user
tableau
$lookup
avec language
collecte et retour en languages
champ
$map
pour itérer l'apparence de id_user.language
tableau
$reduce
pour itérer la boucle de languages
tableau renvoyé par la collection, vérifiez la condition si language_id
correspond puis renvoie name
db.cvsubmit.aggregate([
{
$lookup: {
from: "user",
localField: "id_user",
foreignField: "_id",
as: "id_user"
}
},
{ $unwind: "$id_user" },
{
$lookup: {
from: "language",
localField: "id_user.language.language_id",
foreignField: "_id",
as: "languages"
}
},
{
$addFields: {
languages: "$$REMOVE",
"id_user.language": {
$map: {
input: "$id_user.language",
as: "l",
in: {
_id: "$$l._id",
level: "$$l.level",
name: {
$reduce: {
input: "$languages",
initialValue: "",
in: {
$cond: [
{ $eq: ["$$this._id", "$$l.language_id"] },
"$$this.name",
"$$value"
]
}
}
}
}
}
}
}
}
])
Aire de jeux