mongoose怎么populate一个对象数组里的某个属性字段
展开全部
ASchema=newmongoose.Schema({name:String,B:[BSchema]});varBSchema=newSchema({name:String});varCSchema=newSchema({name:String,B:{type:Schema.ObjectId,ref:'BSchema'}});//ThisisOK,butnotonetimemongoosesearch:CSchema.find({name:'C_xxx'}).exec(function(err,docC){docC.forEach(function(o){varBId=o.B;A.findOne({'B._id':BId},{'B.$':1}).exec(function(err,docA){varBname=docA.B[0].name;varCname=docA.name;});});});//wronginpopulate:CSchema.find({name:'C_xxx'}).populate('ASchema.B').exec(function(err,docC){docC.forEach(function(o){varBname=o.B.name;//ERROR:o.BisaobjectId,o.B.nameisundefined//Anamecan'tfind});});
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询