$lookup
3.6 sintaks memungkinkan Anda untuk bergabung dengan tabel bersarang dan $unwind
untuk mendekonstruksi bidang array dari dokumen input ke output dokumen untuk setiap elemen. Sesuatu seperti ini
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" }
])
Dengan versi 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" }
}}
])