假设以下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}
});
当我查询汽车时,我可以填充零件:
Car.find().populate('partIds').exec(function(err, cars) {
// list of cars with partIds populated
});
在mongoose中是否有一种方法可以填充所有车辆的嵌套部件对象中的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这样的库来为每个populate调用进行等待,直到所有操作完成,然后才返回。
是否可以在不遍历所有汽车的情况下完成?
populate
调用中应该是model: 'Other'
。 - JohnnyHKCar.find().deepPopulate('partIds.otherIds').exec(...)
。(免责声明:我是作者。) - Buu