I have the following document:
{
"_id" : ObjectId("59299670266cc82a042f3817"),
"_userId" : ObjectId("590a08dba07c1a1bee87b310"),
"name" : "My home",
"floors" : [
{
"_id" : ObjectId("59299670266cc82a042f4541"),
"name" : "Floor 1",
"rooms" : [
{
"_id" : ObjectId("59299670266cc82a042f4551"),
"name" : "Room 1",
"devices" : [
{
"_id" : ObjectId("59299670266cc82a042f4561"),
"name" : "Device 1"
},
{
"_id" : ObjectId("59299670266cc82a042f4562"),
"name" : "Device 2"
}
]
},
{
"_id" : ObjectId("59299670266cc82a042f4552"),
"name" : "Room 2",
"devices" : [
{
"_id" : ObjectId("59299670266cc82a042f4563"),
"name" : "Device 3"
},
{
"_id" : ObjectId("59299670266cc82a042f4564"),
"name" : "Device 4"
}
]
}
]
},
{
"_id" : ObjectId("59299670266cc82a042f4542"),
"name" : "Floor 2",
"rooms" : [
{
"_id" : ObjectId("59299670266cc82a042f4553"),
"name" : "Room 1",
"devices" : [
{
"_id" : ObjectId("59299670266cc82a042f4565"),
"name" : "Device 5"
},
{
"_id" : ObjectId("59299670266cc82a042f4566"),
"name" : "Device 6"
}
]
},
{
"_id" : ObjectId("59299670266cc82a042f4554"),
"name" : "Room 2",
"devices" : [
{
"_id" : ObjectId("59299670266cc82a042f4567"),
"name" : "Device 7"
},
{
"_id" : ObjectId("59299670266cc82a042f4568"),
"name" : "Device 8"
}
]
}
]
}
]
}
And I would like to get a list of all devices:
[
{
"_id" : ObjectId("59299670266cc82a042f4561"),
"name" : "Device 1"
},
{
"_id" : ObjectId("59299670266cc82a042f4562"),
"name" : "Device 2"
},
{
"_id" : ObjectId("59299670266cc82a042f4563"),
"name" : "Device 3"
},
{
"_id" : ObjectId("59299670266cc82a042f4564"),
"name" : "Device 4"
},
{
"_id" : ObjectId("59299670266cc82a042f4565"),
"name" : "Device 5"
},
{
"_id" : ObjectId("59299670266cc82a042f4566"),
"name" : "Device 6"
},
{
"_id" : ObjectId("59299670266cc82a042f4567"),
"name" : "Device 7"
},
{
"_id" : ObjectId("59299670266cc82a042f4568"),
"name" : "Device 8"
}
]
Is that possible with MongoDB?
I've tried it with aggregate and { $unwind: $floors.rooms.devices }
or
{ $unwind: $floors }
{ $unwind: $floors.rooms }
{ $unwind: $floors.rooms.devices }
but the output is always empty. I don't know if I would get the output I want with $unwind at all.
Because I'm also using aggregate for other things a solution with it would be nice.
via Kiltarc
No comments:
Post a Comment