我使用Mongoose.js并且无法解决3级别层次结构文档的问题。猫鼬填充嵌入式
有两种方法可以做到这一点。
First - without refs。
C = new Schema({
'title': String,
});
B = new Schema({
'title': String,
'c': [C]
});
A = new Schema({
'title': String,
'b': [B]
});
我需要显示C记录。我如何填充/找到它,只知道C的_id?
我尝试使用:
A.findOne({'b.c._id': req.params.c_id}, function(err, a){
console.log(a);
});
但我不知道如何从returnet得到一个对象只C对象,我需要。
二如果与裁判工作:
C = new Schema({
'title': String,
});
B = new Schema({
'title': String,
'c': [{ type: Schema.Types.ObjectId, ref: 'C' }]
});
A = new Schema({
'title': String,
'b': [{ type: Schema.Types.ObjectId, ref: 'B' }]
});
如何填充所有B,C的记录得到的层次结构?
我尝试使用这样的:
A
.find({})
.populate('b')
.populate('b.c')
.exec(function(err, a){
a.forEach(function(single_a){
console.log('- ' + single_a.title);
single_a.b.forEach(function(single_b){
console.log('-- ' + single_b.title);
single_b.c.forEach(function(single_c){
console.log('--- ' + single_c.title);
});
});
});
});
但它会返回未定义single_c.title。我有办法填充它吗?
谢谢。
将是一件好事,现在,这是支持选择一个新的接受的答案。 – JohnnyHK