一、Mongoose populate
官方文档 https://mongoosejs.com/docs/populate.html
二、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)
})