hi all,
I have the following school document from the Schools collection:
{
"name": "school1",
"sections": [
{
"name": "section1",
"cabinets": [
{
"name": "cabinet1",
"columns": [
{
"_id": "1"
"index": 1,
}
]
}
]
}
]
}
and the following locker document from the Lockers collection.
each locker belongs to one specific column in a cabinet
{
"_id": "...",
"columnId": "1"
},
{
"_id": "...",
"columnId": "1"
},
{
"_id": "...",
"columnId": "1"
}
i want to query all schools and join each column with its corresponding lockers like so:
{
"name": "school1",
"sections": [
{
"name": "section1",
"cabinets": [
{
"name": "cabinet1",
"columns": [
{
"_id": "1",
"index": 1,
"lockers": [
{
"_id": "...",
"columnId": "1"
},
{
"_id": "...",
"columnId": "1"
},
{
"_id": "...",
"columnId": "1"
}
]
}
]
}
]
}
]
}
i tried to achieve it using aggregate and $lookup, the thing is the results are unwind, meaning for each section, cabinet and column i get a separate school document.
how do i aggregate properly and retain the structure of school with all its arrays and still get for every column its corresponding lockers array
here is a sample of my invalid aggregation:
https://mongoplayground.net/p/CE-2YxHZWP8
thanks!