2015-01-27 13 views
15

die folgenden 3 Modelle Angenommen:Mongoose bevölkern verschachtelten Array

var CarSchema = new Schema({ 
    name: {type: String}, 
    partIds: [{type: Schema.Types.ObjectId, ref: 'Part'}], 
}); 

var PartSchema = new Schema({ 
    name: {type: String}, 
    otherIds: [{type: Schema.Types.ObjectId, ref: 'Other'}], 
}); 

var OtherSchema = new Schema({ 
    name: {type: String} 
}); 

Wenn ich für Autos abfragen kann ich die Teile füllen:

Car.find().populate('partIds').exec(function(err, cars) { 
    // list of cars with partIds populated 
}); 

Gibt es eine Möglichkeit in Mungo die otherIds in dem aufzufüllen verschachtelte Teile Objekte für alle Autos.

Car.find().populate('partIds').exec(function(err, cars) { 
    // list of cars with partIds populated 
    // Try an populate nested 
    Part.populate(cars, {path: 'partIds.otherIds'}, function(err, cars) { 
    // This does not populate all the otherIds within each part for each car 
    }); 
}); 

ich wahrscheinlich über jedes Auto laufen kann und versuchen, bevölkern zu:

Car.find().populate('partIds').exec(function(err, cars) { 
    // list of cars with partIds populated 

    // Iterate all cars 
    cars.forEach(function(car) { 
    Part.populate(car, {path: 'partIds.otherIds'}, function(err, cars) { 
     // This does not populate all the otherIds within each part for each car 
    }); 
    }); 
}); 

Problem ist, dass ich eine lib wie async zu verwenden haben die bevöl Aufruf für jeden zu machen und bis alle warten sind fertig und dann zurück.

Kann man ohne Schleife über alle Autos tun?

+0

diese bevöl Methode findet in Ihrer get/post Route korrekt? – Winnemucca

Antwort

29

Update: Bitte sehen Trinh Hoang Nhu's answer eine kompaktere Version, die in Mongoose 4. Zusammengefasst unten hinzugefügt wurde:

Car 
    .find() 
    .populate('partIds') 
    .exec(function(err, docs) { 
    if(err) return callback(err); 
    Car.populate(docs, { 
     path: 'partIds.otherIds', 
     model: 'Other' 
    }, 
    function(err, cars) { 
     if(err) return callback(err); 
     console.log(cars); // This object should now be populated accordingly. 
    }); 
    }); 
:

Car 
    .find() 
    .populate({ 
    path: 'partIds', 
    model: 'Part', 
    populate: { 
     path: 'otherIds', 
     model: 'Other' 
    } 
    }) 

Mungo 3 und unten Bei verschachtelten Populationen wie diesem müssen Sie dem Schema, das Sie popu wollen, mongoose sagen spät aus.

+0

Crap RTFM;) Das sehe ich jetzt. Aber egal, was ich versuche, ich bekomme ein leeres Array für andere IDs zurück. Als Plausibilitätsprüfung Wenn ich die Populate-Methode nicht aufruft, gibt es ein Array von IDs (was korrekt ist). Füllen füllt diese IDs nicht auf. Running 3.8.22 am längsten von npm – lostintranslation

+0

Auch Protokollierung aktiviert. Ich sehe keinen weiteren Aufruf an die andere Sammlung, um den "Join" zu füllen. So macht Mungo nicht einmal den Anruf. – lostintranslation

+2

@lostintranslation Es sollte 'Modell: 'Other' in diesem verschachtelten' populate' Aufruf sein. – JohnnyHK

21

Mongoose 4 unterstützt diese

Car 
.find() 
.populate({ 
    path: 'partIds', 
    model: 'Part', 
    populate: { 
    path: 'otherIds', 
    model: 'Other' 
    } 
}) 
+2

Hinweis: Modell: 'Andere' ist notwendig. Ohne es gibt es leere Array. Docs gibt es nicht an: http://mongoosejs.com/docs/populate.html#deep-populate –

Verwandte Themen