mongoose連表查詢2

1.解讀mongoose的populatejavascript

    1.Query#populatehtml

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的類型是:ObjectIdNumberString, and Buffer are valid for use as refs.
那麼
_id的類型是:ObjectIdNumberStringBuffer_crestor 存儲的類型必須與這個_id的數據類型一直,只能關聯_id.
在文檔關聯使用ref必定要注意,關聯的那個model只能匹配_id這個字段,你要是搞個自動生成的啥的一律無效 
相關文章
相關標籤/搜索