我用mongoose .js,无法解决三级层次 struct 文档的问题.
有两种方法.
First-没有裁判.
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);
});
但是我不知道如何从退货中得到一个我需要的东西.
Second如果使用参考文献:
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记录以获得层次 struct ?
我试着用这样的东西:
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);
});
});
});
});
但它将返回未定义的单c标题.我有办法填充它吗?
谢谢