为了账号安全,请及时绑定邮箱和手机立即绑定

mongoose如何使用populate填充二维数组

mongoose如何使用populate填充二维数组

慕盖茨4494581 2018-11-14 13:14:18
结构如下,points是一个二维数组,如何使用populate填充这个数组。 points: [[{    type: ObjectId,    ref: 'point'  }]]
查看完整描述

1 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

var _User = new Schema({

    _id:Number,// 只支持ObjectId,Number,String,Buffer,就这几个引用类型,ref匹配的只有这个_id

    name:String,

    age:Number

});


var _Comment = new Schema({

    comments:[{

        text:String,

        created_by:{type:Number,ref:'User'}//这个User是model名称,数据类型要于_id的数据类型一致。

    }]

})


var userModel = mongoose.model('User',_User);

var commentsModel = mongoose.model('Comment',_Comment);


// 查询


commentModel.findOne({ })

    .populate('comments.created_by')

    .exec(function (err, commets) {

            console.log(err,commets);

    })


查看完整回答
反对 回复 2018-12-08
  • 1 回答
  • 0 关注
  • 509 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号