我正在使用ElasticSearch和mongoosastic来同步MongoDB和ElasticSearch之间的数据。我希望包含一个模式的属性,这是我研究中的另一个对象:我想要显示具有我正在搜索的类别的文章。 这些是我的2个模式:ArticleSchema和CategorySchema。文章包含一个名为“Categorie”的Category对象。
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
}
});
答案 0 :(得分:1)
您的ArticleSchema
定义只需要正确声明其categorie
属性类型(即不是Schema.ObjectId
而是CategorySchema
)。
如mongoosastic docs 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'
}
});
答案 1 :(得分: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'}
]
})
注意事项: