Ich mag würde building
mit floors
und rooms
aggregieren:MongoDB Aggregation Lookup
Angesichts der folgenden drei Sammlungen:
/* buildings */
{
"_id" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "home"
}
/* floors */
{
"_id" : ObjectId("59a09abe388f595b15bb5fa3"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "upstairs"
}
{
"_id" : ObjectId("59a09abe388f595b15bb5fa2"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "downstairs"
}
/* rooms */
{
"_id" : ObjectId("59a09bce388f595b15bb5fb6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "bathroom",
"_userId" : ObjectId("590a08dba07c1a1bee87b310")
}
{
"_id" : ObjectId("59a09bce388f595b15bb5fc6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "living room",
"_userId" : ObjectId("590a08dba07c1a1bee87b310")
}
würde ich sie gerne zusammen Nachschlag und versucht, es mit der folgenden Abfrage:
db.getCollection('buildings').aggregate([
{
"$lookup": {
"from": "floors",
"localField": "_id",
"foreignField": "buildingId",
"as": "floors"
}
},
{
"$lookup": {
"from": "rooms",
"localField": "floors._id",
"foreignField": "floorId",
"as": "floors.rooms"
}
}
]);
Dies ergibt folgendes Ergebnis:
{
"_id" : ObjectId("59a09abe388f595b15bb5fa6"),
"_userId" : ObjectId("590a08dba07c1a1bee87b310"),
"name" : "home",
"floors" : {
"rooms" : []
}
}
Aber ich würde folgendes Ergebnis haben:
{
"_id" : ObjectId("59a09abe388f595b15bb5fa6"),
"_userId" : ObjectId("590a08dba07c1a1bee87b310"),
"name" : "home",
"floors" : [
{
"_id" : ObjectId("59a09abe388f595b15bb5fa3"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "upstairs",
"rooms": [
{
"_id" : ObjectId("59a09bce388f595b15bb5fb6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "bathroom"
},
{
"_id" : ObjectId("59a09bce388f595b15bb5fc6"),
"floorId" : ObjectId("59a09abe388f595b15bb5fa3"),
"name" : "living room"
}
]
},
{
"_id" : ObjectId("59a09abe388f595b15bb5fa2"),
"buildingId" : ObjectId("59a09abe388f595b15bb5fa6"),
"name" : "downstairs",
"rooms" : [ ]
}
]
}
Wie Sie sehen, ich würde alle Verweise auf Nachschlag mag die Baustruktur erhalten damit Etagen und Zimmer ist.
Wie kann ich das erreichen?
können Sie Ihre Antwort als richtig für neue Leser akzeptieren. – RLD
werde ich, aber ich muss 48 Stunden warten, nachdem ich die Frage gestellt habe, bis ich das tun kann. – Hativ