2017-02-21 3 views
0

Ich benutze sowohl Mongoose und Mongosastic in einer Express-App, ich habe Mongoose Schema course enthält 2 Referenzen trainer und course-category. Hier ist meine Modelle Definition: Trainer:Empty Mongoosastic Referenz nach Aktualisierung Dokument

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) 

Und schließlich Kurs:

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' 
     } 
    ] 
}) 

Wenn ich spare einen Kurs zum ersten Mal , Ich bekomme den doc indiziert in elasticsearch und das Trainerfeld enthält alle Ausgewählte Eigenschaften außer der _id und ein leeres doc für das Feld courseCategory. Wenn ich den gleichen Kurs aktualisiere, bekomme ich einen leeren Trainer und eine courseCategory.

Und hier ist eine Probe eines Kursdaten Ich habe versucht,/Update hinzufügen mit dieser API:

{ 
    "trainer": { 
     "_id": "58ac661bac2f3e6724d9cd02", 
     "firstname": "Jemli", 
     "lastname": "Fathi", 
     "email": "[email protected]", 
     "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

Welche Funktion verwenden Sie zum Aktualisieren von Dokumenten? – Edgar

+0

@Edgar Ich benutze findOneAndUpdate mit {new: true} Option – jemlifathi

+0

Stellen Sie Array in HydratOptions Option befüllen. Ich denke, es wird das Problem beheben. – Edgar

Antwort

0

Das Problem war, dass ich vergessen, meine Referenzen zu füllen wie @Edgar sagte, benutzte ich

Course.findOneAndUpdate (...). bevölkern ('Trainer'). bevölkern ('courseCategory')

zu meinem Trainer und courceCategory Referenzen und everybevöl g funktioniert jetzt einwandfrei

Verwandte Themen