var mongoose = require('mongoose'); var QcmQuestionSchema = new mongoose.Schema({ num: Number, libelle: String, choices: [ { type: mongoose.Schema.Types.ObjectId, ref: 'QcmChoice' } ] }, {timestamps: true, toJSON: {virtuals: true}}); QcmQuestionSchema.methods.addChoice = function (id) { if (this.choices.indexOf(id) === -1) { this.choices.push(id); } return this.save(); }; QcmQuestionSchema.methods.removeChoice = function (id) { this.choices.remove(id); return this.save(); }; QcmQuestionSchema.methods.toJSONFor = function() { return { num: this.num, libelle: this.libelle, choices: this.choices.map(choice => choice.toJSONFor()) }; }; mongoose.model('QcmQuestion', QcmQuestionSchema);