假设以下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 这样的库来为每个库进行填充调用,然后等到所有操作都完成然后返回。
可以不循环所有汽车吗?