$lookup
3.6 syntax låter dig gå med i kapslade tabeller och $unwind
to dekonstruerar ett matrisfält från inmatningsdokumenten för att mata ut ett dokument för varje element. Något sånt här
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" }
])
Med 3.4-versionen
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" }
}}
])