Предполагая, что следующие 3 модели:Mongoose заполнить вложенный массив
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}
});
Когда я запрашиваю для автомобилей я могу заселить части:
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
});
});
});
Проблема есть в том, что я должен использовать LIB, как в асинхронном, чтобы сделать вызов заселить для каждого и ждать, пока все и затем вернуться.
Можно ли обойти все автомобили?
метод это Заселите происходит внутри вашего ПОЛУЧИТЬ/пост маршрут правильно? – Winnemucca