usermodel:
{
"_id" : ObjectId("5a2622b0004be35f4baf70e3"),
"username": "testuser",
"bankdetails" : [
{
"_id" : ObjectId("5a294146015da72db1560326"),
"bankname" : "bankofamerica"
}
]
}
samplemodel:
{
"_id" : ObjectId("5a41f785416d0d3d93794ca9"),
"bankid" : ObjectId("5a294146015da72db1560326"),
}
samplemodel.find().populate('bankid').exec(function(err,resData){
res.json(resData);
});
我已经使用了上面的代码但没有使用这段代码......请给出解决这个问题的解决方案.....
答案 0 :(得分:1)
您必须更新架构,为银行创建单独的架构。并作为嵌入文档传入“bankdetails”,要从样本模型中获取结果,您需要传递Bank Schema的参考。例如:
var BankSchema = new Schema({
bankname:{ type: String}
});
var Bank = mongoose.model('Bank', BankSchema);
var UserSchema = new Schema({
username:{ type: String},
bankdetails:[BankSchema],
});
var SampleSchema = new Schema({
sameple_string:{ type: String},
bankid:{
type:mongoose.Schema.Types.ObjectId,
ref:'Bank'
},
});