猫鼬复合唯一键索引似乎不起作用

时间:2018-10-22 12:30:21

标签: javascript node.js mongodb mongoose koa

我已经添加了mongoose和mongoDB文档中建议的唯一键索引,但是当我尝试使用insertMany插入多个问题时,它不会在重复项上引发错误。我在这里做错什么了吗?

Question.insertMany(questions);

const NotRequiredStringSchema = { type: String, required: false, default: '' };

const Question = new Schema({
    "type": {
        type: String,
        trim: true,
        enum: Object.values(questionTypes),
        required: 'Question type is required'
    },
    "text": {
        type: String,
        required: true
    },
    "desc": NotRequiredStringSchema,
    "options": [{
        "id": ObjectId,
        "name": NotRequiredStringSchema,
        "helperText": NotRequiredStringSchema,
        "icon_key": NotRequiredStringSchema,
        "icon_url": NotRequiredStringSchema,
        "icon_svg": NotRequiredStringSchema
    }]
});

Question.index({
    type: 1,
    text: 1
}, {
    unique: true
});

const model = mongoose.model('Question', Question);

module.exports = model;

0 个答案:

没有答案