E11000 duplicate key error collection

新手上路,请多包涵
const CommentsSchema = new Schema({
  userId: { type: Schema.Types.ObjectId, ref: 'User', require: true },
  articleId: { type: Schema.Types.ObjectId, ref: 'Article', require: true },
  content: { type: String, require: true },
  created_at: { type: Date, default: Date.now },
  thumbsUp: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  thumbsDown: [{ type: Schema.Types.ObjectId, ref: 'User' }]
})
commentModel = {
  userId: ctx.session.user._id || ctx.request.body.userId,
  articleId: articleId,
  content: content
};

Comments.create(commentModel)

第二次插入数据的时候报错

  name: 'MongoError',
  message: 'E11000 duplicate key error collection: myblog.comments index: thumbsUp_1 dup key: { : undefined }',
  driver: true,
  code: 11000,
  index: 0,
  errmsg: 'E11000 duplicate key error collection: myblog.comments index: thumbsUp_1 dup key: { : undefined }',
  getOperation: [Function],
  toJSON: [Function],
  toString: [Function] }

请问该如何解决

阅读 2.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进