开发者社区> 问答> 正文

mongoose怎么populate一个对象数组里的某个属性字段?

使用Mongoose时, 我想在User里populate一个对象数组字段里的一个属性q

var Question = new Schema({
  title: {type: String},
  answers: Schema.Types.Mixed, //{A:'',B:''}
  category: {type: String}, 
  type: {type: String},        
  enable: {type: Boolean, default: true},
  sex: Number,             
  //random: {type: Number, default: Math.random}, 
  createdAt: {type: Date, default: Date.now} 
});
var User = new Schema({
  username: {type: String},    
  password: {type: String},
  typeQuestion: [{q: {type: Schema.Types.ObjectId, ref: 'Question'}, a: String,option:String }],    
});

怎么实现?

展开
收起
蛮大人123 2016-06-14 16:35:35 3851 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    User.find({})
        .populate('typeQuestion.q')
        .exec(function(err, user) {
            // callback
        });
    2019-07-17 19:38:06
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载