ExpressJS-重复键错误集合

时间:2018-11-25 16:17:54

标签: javascript node.js mongodb express mongoose

当我使用数据库中存在的类别创建新事件时遇到此错误问题,例如,我创建了一个类别为“ javascript”的事件并将其保存到数据库,然后尝试创建一个新事件类别为“ javascript,html,css”,然后出现此错误重复键错误集合

所以我的事件架构是这样的:

    const EventSchema = new Schema({
    title: {
        type: String,
        required: true,
        min: 3,
        max: 100
    },
    featuredImage: {
        type: Object,
    },
    from: {
        type: Date,
        required: true
    },
    to: {
        type: Date,
        required: true
    },
    location: {
        name: {
            type: String
        },
        address: {
            type: Object
        }
    },
    description: {
        type: String
    },
    categories: {
        type: Array,
        trim: true
    },
    featured: {
        type: Boolean
    },
    created_by: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    slug: {
        type: String,
        default: null
    },
    registration: {
        type: Boolean
    },
    tickets: [],
    allday: {
        type: Boolean
    },
    speakers: [{
        type: Schema.Types.ObjectId,
        ref: 'User'
    }],
    attendees: [{
        type: Schema.Types.ObjectId,
        ref: 'User'
    }],
    comments: [CommentSchema]
}, {
    timestamps: true,
    usePushEach: true
});

所以基本上发送字符串数组,我得到了这个错误。

1 个答案:

答案 0 :(得分:0)

您可能在categories上使用unique标志定义了一些索引

您可以使用db.events.getIndexes()列出现有索引。

您可以放下并重新创建罪魁祸首(请小心):

> db.events.dropIndex({categories:1})
> db.events.ensureIndex({categories:1},{sparse:true})