2015-08-28 60 views
3

我使用ElasticSearch和mongoosastic到的MongoDB和ElasticSearch.I之間同步數據要包括架構這是我的另一個研究對象的屬性:我想有要顯示的類別文章,我正在尋找。 這是我的2種模式:ArticleSchema和CategorySchema。文章包含一個名爲「Categorie」的類別對象。包含的ObjectId在搜索Elasticsearch

var ArticleSchema = new Schema({ 
    created: { 
     type: Date, 
     default: Date.now 
    }, 
    ... 
    user: { 
     type: Schema.ObjectId, 
     ref: 'User' 
    }, 
    categorie: { 
     type: Schema.ObjectId, 
     es_indexed:true, 
     ref: 'Category', 
     required: 'Le champ "Categorie" ne peut pas etre vide' 
    } 
}); 

var CategorySchema = new Schema({ 
    name: { 
     type: String, 
     default: '', 
     required: 'Please fill Category name', 
     trim: true 
    }, 
    ... 
    niveau: { 
     type: Number 
    } 
}); 

回答

1

ArticleSchema定義只需要適當地申報其財產categorie類型(即不Schema.ObjectIdCategorySchema)。

正如mongoosastic文檔for nested models所示,你可以做這樣的:

var ArticleSchema = new Schema({ 
    ... 
    categorie: { 
     type: [CategorySchema],  <--- change the type to this 
     es_indexed:true, 
     ref: 'Category', 
     required: 'Le champ "Categorie" ne peut pas etre vide' 
    } 
}); 
+0

是否有引用由他們的ObjectId文件,而不是作爲一個子文件由模式創建它的另一種方式? IE瀏覽器。像貓鼬的參考和填充()? –

+0

貌似不開箱。我去看看我可以通過水化添加功能,併爲它創建一個PR今晚。你的回答讓我更深入瞭解了這個問題,並認識到嵌套模型和人口之間的差異。謝謝! –

1

我認爲這是你在找什麼https://github.com/mongoosastic/mongoosastic/pull/118

var Comment = new Schema({ 
    title: String, 
    body: String, 
    author: String 
}); 


var User = new Schema({ 
    name: {type:String, es_indexed:true}, 
    email: String, 
    city: String, 
    comments: {type: Schema.Types.ObjectId, ref: 'Comment', es_schema: Comment, es_indexed:true, es_select: 'title body'} 
}) 

User.plugin(mongoosastic, { 
    populate: [ 
     {path: 'comments', select: 'title body'} 
    ] 
}) 

注意事項:

  1. 你應該提供您參考架構es_schema,以作出正確的映射
  2. 默認情況下mongoosastic意願指數整體架構。在模式中提供es_select字段以選擇特定字段。
  3. 的填入陣列是相同的選項數組傳遞給貓鼬Model.populate