MongoDB学习day10--Mongoose的populate实现关联查询

1、Mongoose populate官方文档html

https://mongoosejs.com/docs/populate.htmlmongoose

2、Mongoose populate关联查询spa

 

1.定义ref3d

var ArticleSchema = new Schema({
  title:{
    type: String, unique: true
  },   cid : {     type: Schema.Types.ObjectId,     ref:'ArticleCate' //model 的名称   }, /*分类 id*/   author_id:{     type: Schema.Types.ObjectId,     ref:'User'   }, /*用户的 id*/   author_name:{     type:String   },   descripton:String,   content : String });

2.关联查询code

//三个表关联
ArticleModel.find({}).populate('cid').populate('author_id').exec(function(err,docs){   console.log(docs) })
相关文章
相关标签/搜索