node.js 猫鼬填充嵌套数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28179720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose populate nested array
提问by lostintranslation
Assuming the following 3 models:
假设有以下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}
});
When I query for Cars I can populate the parts:
当我查询汽车时,我可以填充零件:
Car.find().populate('partIds').exec(function(err, cars) {
// list of cars with partIds populated
});
Is there a way in mongoose to populate the otherIds in the nested parts objects for all the cars.
猫鼬有没有办法为所有汽车填充嵌套部件对象中的 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
});
});
I can probably iterate over each car and try to populate:
我可能可以遍历每辆车并尝试填充:
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
});
});
});
Problem there is that I have to use a lib like async to make the populate call for each and wait until all are done and then return.
问题是我必须使用像 async 这样的库来为每个调用 populate 并等待所有完成然后返回。
Possible to do without looping over all cars?
可以不循环遍历所有汽车吗?
回答by Sven
Update:Please see Trinh Hoang Nhu's answerfor a more compact version that was added in Mongoose 4. Summarized below:
更新:请参阅Trinh Hoang Nhu对 Mongoose 4 中添加的更紧凑版本的回答。总结如下:
Car
.find()
.populate({
path: 'partIds',
model: 'Part',
populate: {
path: 'otherIds',
model: 'Other'
}
})
Mongoose 3 and below:
猫鼬 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.
});
});
For nested populations like this, you have to tell mongoose the Schema you want to populate from.
对于这样的嵌套种群,您必须告诉猫鼬您要从中填充的架构。
回答by Trinh Hoang Nhu
Mongoose 4 support this
Mongoose 4 支持这个
Car
.find()
.populate({
path: 'partIds',
model: 'Part',
populate: {
path: 'otherIds',
model: 'Other'
}
})
回答by Riyas TK
Use mongoose deepPopulate plugin
car.find().deepPopulate('partIds.otherIds').exec();
回答by Tu?n Anh ?ào
It's should be better with
它应该更好
Car
.find()
.populate({
path: 'partIds.othersId'
})

