猫鼬填充嵌套数组
Mongoose populate nested array
假设有以下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
});
猫鼬有没有办法在所有汽车的嵌套零件对象中填充 otherId。
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
});
});
});
问题是我必须使用像异步这样的库来为每个调用填充并等待所有完成然后 return。
可以不用遍历所有汽车吗?
更新: 请参阅 以了解在 Mongoose 4 中添加的更紧凑的版本。总结如下:
Car
.find()
.populate({
path: 'partIds',
model: 'Part',
populate: {
path: 'otherIds',
model: 'Other'
}
})
Mongoose 3 及以下版本:
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.
});
});
对于这样的嵌套群体,您必须告诉 mongoose 您要从中填充的架构。
使用mongoose deepPopulate plugin
car.find().deepPopulate('partIds.otherIds').exec();
Mongoose 4 支持这个
Car
.find()
.populate({
path: 'partIds',
model: 'Part',
populate: {
path: 'otherIds',
model: 'Other'
}
})
搭配
应该会更好
Car
.find()
.populate({
path: 'partIds.othersId'
})
假设有以下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
});
猫鼬有没有办法在所有汽车的嵌套零件对象中填充 otherId。
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
});
});
});
问题是我必须使用像异步这样的库来为每个调用填充并等待所有完成然后 return。
可以不用遍历所有汽车吗?
更新: 请参阅
Car
.find()
.populate({
path: 'partIds',
model: 'Part',
populate: {
path: 'otherIds',
model: 'Other'
}
})
Mongoose 3 及以下版本:
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.
});
});
对于这样的嵌套群体,您必须告诉 mongoose 您要从中填充的架构。
使用mongoose deepPopulate plugin
car.find().deepPopulate('partIds.otherIds').exec();
Mongoose 4 支持这个
Car
.find()
.populate({
path: 'partIds',
model: 'Part',
populate: {
path: 'otherIds',
model: 'Other'
}
})
搭配
应该会更好Car
.find()
.populate({
path: 'partIds.othersId'
})