I have following document structure:
{
clientId: 1001
buildings: [
_id: campus1
rooms: [
{
_id: 2001
name: conference
},
{
_id: 2002
name: meeting
},
]
]
}
I would like to get a flattened version of all room ids along with their parent ids:
{
roomId: 2001
buildingId: campus1
},
{
roomId: 2001
clientId: 1001
},
{
roomId: 2002
buildingId: campus1
},
{
roomId: 2002
clientId: 1001
}
That would be starting from the room id, going one level up and printing the room id and the building Id. After that again starting from the room id but this time going 2 levels up and printing the room id together with the client id.
Is there a way of achieving this?
With what I have so far I'm not getting the desired results:
db.collection.aggregate(
[
{ $unwind:"$buildings" },
{ $unwind:"$building.rooms" },
{
$project: {
_id: 0,
client: "$_id",
building: "$buildings._id",
room: "$buildings.floors.rooms._id",
}
},
{ $unwind:"$room" }
]
)