次の 3 つのモデルを想定しています。
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}
});
Cars をクエリすると、パーツを入力できます。
Car.find().populate('partIds').exec(function(err, cars) {
// list of cars with partIds populated
});
すべての車のネストされた部品オブジェクトに otherIds を設定する方法はマングースにありますか?
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
});
});
おそらく、各車を反復処理して、データを入力しようとすることができます。
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
});
});
});
問題があるのは、async のような lib を使用してそれぞれの populate 呼び出しを行い、すべてが完了するまで待ってから戻る必要があることです。
すべての車をループせずに行うことは可能ですか?