【发布时间】:2018-11-26 01:42:12
【问题描述】:
当我使用我的数据库中存在的类别创建一个新事件时,我遇到了这个错误的问题,例如,我创建了一个类别为“javascript”的事件并将其保存到数据库,然后我尝试创建一个新事件使用“javascript、html、css”类别,然后我收到此错误duplicate key error collection
所以我的事件架构是这样的:
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
});
所以基本上发送字符串数组,我得到了这个错误。
【问题讨论】:
-
其他字段是唯一的吗?看看这个stackoverflow.com/questions/24430220/…
-
请举例说明您创建的两个事件(创建的事件和引发错误的事件)
标签: javascript node.js mongodb express mongoose