1.解读mongoose的populate
1.
var mongoose = require('mongoose') , Schema = mongoose.Schema var personSchema = Schema({ _id : Number, // 只支持ObjectId,Number,String,Buffer,就这几个引用类型,ref匹配的只有这个_id name : String,
age : Number, stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }] }); var storySchema = Schema({ _creator : { type: Number, ref: 'Person' }, title : String, fans : [{ type: Number, ref: 'Person' }] }); var Story = mongoose.model('Story', storySchema); var Person = mongoose.model('Person', personSchema); _id的类型是:ObjectId, Number, String, and Buffer are valid for use as refs. 那么_crestor 存储的类型必须与这个_id的数据类型一直,只能关联_id.
在文档关联使用ref一定要注意,关联的那个model只能匹配_id这个字段,你要是搞个自动生成的啥的一概无效