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

一、Mongoose populate官方文档

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

二、Mongoose populate关联查询

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

1.定义ref

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.关联查询

//三个表关联
ArticleModel.find({}).populate('cid').populate('author_id').exec(function(err,docs){   console.log(docs) })