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) })
相關文章
相關標籤/搜索