2017-09-10 3 views
2

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?

Antwort

0

$unwind macht Etagen $lookup Lage und $group fügt sie wieder zusammen.

db.getCollection('buildings').aggregate([ 
    { 
     "$lookup": { 
      "from": "floors", 
      "localField": "_id", 
      "foreignField": "buildingId", 
      "as": "floors" 
     } 
    }, 
    { 
     "$unwind": "$floors" 
    }, 
    { 
     "$lookup": { 
      "from": "rooms", 
      "localField": "floors._id", 
      "foreignField": "floorId", 
      "as": "floors.rooms" 
     } 
    }, 
    { 
     "$group": { 
      "_id": "$_id", 
      "name": { "$first": "$name" }, 
      "floors": { "$push": "$floors" } 
     } 
    }]); 

Das Ergebnis ist genau das, was ich angefordert habe (siehe Frage).

+0

können Sie Ihre Antwort als richtig für neue Leser akzeptieren. – RLD

+0

werde ich, aber ich muss 48 Stunden warten, nachdem ich die Frage gestellt habe, bis ich das tun kann. – Hativ

1

Ein Blick auf Ihre Aggregat Abfrage und die die Art der Ausgabe, die es ergibt macht mich doppelt $ Lookup (Lookup in einem anderen Lookup) wird nicht unterstützt durch MongoDB (bis 3.4 Version). Sie sollten also $ verwenden und die Ergebnisse Ihren Erwartungen näher bringen. Hier

ist die Abfrage:

db.getCollection('buildings').aggregate([ 
    { 
     "$lookup": { 
      "from": "floors", 
      "localField": "_id", 
      "foreignField": "buildingId", 
      "as": "floors" 
     } 
    }, 
    {"$unwind":"$floors"}, 
    { 
     "$lookup": { 
      "from": "rooms", 
      "localField": "floors._id", 
      "foreignField": "floorId", 
      "as": "floors.rooms" 
     } 
    } 
    ]); 

und seine Ausgabe:

{ 
     "_id" : ObjectId("59a09abe388f595b15bb5fa6"), 
     "name" : "home", 
     "floors" : { 
       "_id" : ObjectId("59a09abe388f595b15bb5fa3"), 
       "buildingId" : ObjectId("59a09abe388f595b15bb5fa6"), 
       "name" : "upstairs", 
       "rooms" : [ 
         { 
           "_id" : ObjectId("59a09bce388f595b15bb5fb6"), 
           "floorId" : ObjectId("59a09abe388f595b15bb5fa3"), 
           "name" : "bathroom", 
           "_userId" : ObjectId("590a08dba07c1a1bee87b310") 
         }, 
         { 
           "_id" : ObjectId("59a09bce388f595b15bb5fc6"), 
           "floorId" : ObjectId("59a09abe388f595b15bb5fa3"), 
           "name" : "living room", 
           "_userId" : ObjectId("590a08dba07c1a1bee87b310") 
         } 
       ] 
     } 
} 
{ 
     "_id" : ObjectId("59a09abe388f595b15bb5fa6"), 
     "name" : "home", 
     "floors" : { 
       "_id" : ObjectId("59a09abe388f595b15bb5fa2"), 
       "buildingId" : ObjectId("59a09abe388f595b15bb5fa6"), 
       "name" : "downstairs", 
       "rooms" : [ ] 
     } 
}