我在Express应用程序中使用Mongoose和Mongoosastic,我有Mongoose Schema course
包含2个引用trainer
和course-category
。这是我的模型定义:
的师:
let TrainerSchema = new Schema({
firstname: {
type: String,
required: true,
es_indexed: true,
es_index: 'not_analyzed',
es_type: 'string'
}...
}, { versionKey: false })
TrainerSchema.plugin(mongoosastic, {
'index': 'training-hub',
'type': 'trainers',
'host': 'localhost',
'port': '9200'
})
let Model = mongoose.model('trainer', TrainerSchema)
exports.Schema = TrainerSchema
CourseCategory:
let CourseCategorySchema = new Schema({
name: {
type: String,
required: true,
unique: true,
es_type: 'multi_field',
es_indexed: true,
es_fields: {
name: {type: 'string', index: 'analyzed'},
raw: {type: 'string', index: 'not_analyzed'}
}
}...
}, { versionKey: false })
CourseCategorySchema.plugin(mongoosastic, {
'index': 'training-hub',
'type': 'course-categories',
'host': 'localhost',
'port': '9200'
})
exports.Schema = CourseCategorySchema
let Model = mongoose.model('course-category', CourseCategorySchema)
最后,课程:
let CourseSchema = new Schema({
title: {
type: String,
required: true,
es_indexed: true,
es_index: 'analyzed',
es_type: 'string'
}...
trainer: {
type: Schema.Types.ObjectId,
ref: 'trainer',
required: true,
es_indexed: true,
es_schema: Trainer,
es_select: 'firstname lastname degree nationality slug photo'
},
courseCategory: {
type: Schema.Types.ObjectId,
ref: 'course-category',
required: true,
es_indexed: true,
es_schema: CourseCategory,
es_select: 'name'
}
}, {versionKey: false})
CourseSchema.index({'trainer': 1, 'title': 1}, {unique: true})
CourseSchema.plugin(mongoosastic, {
'index': 'training-hub',
'type': 'courses',
'host': 'localhost',
'port': '9200'
}, {
populate: [
{
path: 'trainer', select: '_id firstname lastname degree nationality slug photo'
},
{
path: 'courseCategory', select: '_id name'
}
]
})
当我第一次保存课程时,我获得了elasticsearch索引的文档和包含所有选定属性的培训师字段,除了_id和courseCategory字段的空文档,当更新相同的课程时,我得到空培训师和courseCategory。
以下是我尝试使用此API添加/更新的课程数据示例:
{
"trainer": {
"_id": "58ac661bac2f3e6724d9cd02",
"firstname": "Jemli",
"lastname": "Fathi",
"email": "jemlifathi2013@gmail.com",
"photo": "https://dummyimage.com/256x256&text=JemliFathi",
"slug": "Jemli-Fathi",
"updatedAt": "2017-02-21T16:08:59.446Z",
"createdAt": "2017-02-21T16:08:59.447Z",
"trainingExperiences": [],
"experiences": [],
"studies": [],
"nationality": ""
},
"requirements": [
"Basic knowledge of Web development",
"Basic HTML5 skills"
],
"audience": [
"Web developers"
],
"title": "Getting Started with Vue.js",
"courseCategory": {
"_id": "58ac667aac2f3e6724d9cd03",
"name": "Web Development",
"description": "Design and development of Websites",
"createdAt": "2017-02-21T16:10:34.875Z"
},
"description": "This course will help you get started with Vue.js. It includes Vue.js basics, Consuming external resources with Vue Resource, Routing using Vue Router and store management using Vuex",
"format": [
"Intra-entreprise",
"Inter-entreprise"
],
"duration": 4,
"price": 222,
"language": "EN",
"scheduleUrl": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487696105/training-hub/mpal42fdrcbbhzgmco4w.png",
"cover": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487696109/training-hub/nvl7pqed9q864gr1ljol.png",
"slug": "Getting-Started-with-Vuejs--by--Jemli-Fathi"
}
答案 0 :(得分:0)
问题是我忘了填写我的引用,比如@Edgar说,我用了
Course.findOneAndUpdate(...)填充('训练者&#39)。填充(' courseCategory&#39)
填充我的教练和courceCategory引用,现在一切正常