0

我在 Express 应用程序中同时使用 Mongoose 和 Mongoosastic,我有 Mongoose Schema course,其中包含 2 个引用trainercourse-category. 这是我的模型定义: Trainer

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

课程类别:

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 中索引的文档和包含所有选定属性的 trainer 字段,除了 _id 和 courseCategory 字段的空文档,当更新同一门课程时,我得到空的 trainer 和 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"
  }
4

1 回答 1

0

问题是我忘记像@Edgar 说的那样填写我的参考资料,我用过

Course.findOneAndUpdate(...).populate('trainer').populate('courseCategory')

填充我的培训师和 courceCategory 参考资料,现在一切正常

于 2017-02-24T10:11:47.190 回答