$lookup
3.6 la syntaxe vous permet de joindre des tables imbriquées et $unwind
pour déconstruire un champ de tableau à partir des documents d'entrée pour générer un document pour chaque élément. Quelque chose comme ça
position.aggregate([
{ "$lookup": {
"from": "companies",
"let": { "companyId": "$company_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", "$$companyId" ] } } },
{ "$lookup": {
"from": "industries",
"let": { "industry_id": "$industry_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", "$$industry_id" ] } } }
],
"as": "industry"
}},
{ "$unwind": "$industry" }
],
"as": "company"
}},
{ "$unwind": "$company" }
])
Avec la version 3.4
position.aggregate([
{ "$lookup": {
"from": "companies",
"localField": "company_id",
"foreignField": "_id",
"as": "companies"
}},
{ "$unwind": "$companies" },
{ "$lookup": {
"from": "industries",
"localField": "companies.industry_id",
"foreignField": "_id",
"as": "companies.industry"
}},
{ "$unwind": "$companies.industry" },
{ "$group": {
"_id": "$_id",
"companies": { "$push": "$companies" }
}}
])