一、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) })

 

相关文章:

  • 2022-12-23
  • 2021-09-14
  • 2021-06-17
  • 2022-12-23
  • 2022-01-15
  • 2021-07-21
  • 2021-11-05
  • 2021-07-03
猜你喜欢
  • 2018-01-21
  • 2021-12-28
  • 2021-11-30
  • 2021-08-10
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
相关资源
相似解决方案