【问题标题】:MongoDB - Trying to save multiple documents results in duplicate key error for a schema's ObjectId despite no unique key setMongoDB - 尽管没有唯一键集,但尝试保存多个文档会导致模式的 ObjectId 出现重复键错误
【发布时间】:2018-05-03 10:54:20
【问题描述】:

我得到了这两个模式:

const Account = mongoose.model('Account', new Schema({
  name: {type: String, default: '', required: true},
  email: {type: String, default: '', unique: true, required: true},
  hashed_password: {type: String, default: '', select: false, required: true},
  role: {type: mongoose.Schema.Types.ObjectId, ref: 'Role', required: true},
  organisations: [{type: mongoose.Schema.Types.ObjectId, ref: 'Organisation'}],
  updatedAt: {type: Date, default: null},
  createdAt: {type: Date, default: new Date(), required: true}
}));

const Organisation = mongoose.model('Organisation', new Schema({
  name: {type: String, default: '', required: true, unique: true},
  email: {type: String, default: '', required: true},
  accounts: {type: [{type: mongoose.Schema.Types.ObjectId, ref: 'Account'}], required: true, default: []},
  declinedInvites: {type: [{type: mongoose.Schema.Types.ObjectId, ref: 'Account'}], default: []},  
  admins: {type: [{type: mongoose.Schema.Types.ObjectId, ref: 'Account'}], required: true, default: []},
  updatedAt: {type: Date, default: null},
  updatedBy: {type: mongoose.Schema.Types.ObjectId, ref: 'Account', default: null},
  createdAt: {type: Date, default: new Date(), required: true},
  createdBy: {type: mongoose.Schema.Types.ObjectId, ref: 'Account', default: null, required: true}
}));

如果我将一个 Organisation 保存到数据库中,然后尝试使用此路由保存另一个:

router.post('/organisations', auth.verifyToken, (req, res, next) => {

  const creator = req.decoded._doc._id;

  const organisation = new Organisation({
    name: req.body.name,
    email: req.body.email,
    accounts: [creator],
    declinedInvites: [],
    admins: [creator],
    createdBy: creator
  });

  organisation.save((err, organisation) => {

    if (err) {
      return next(err);
    }
  });
});

它会抛出如下错误:

MongoError: E11000 duplicate key error collection: docs.organisations index: accounts_1 dup key: { : ObjectId('5a0d89e89141e410a9617746') }

空数组declinedInvites也是如此,它会抛出一个错误说同样的事情但是:

MongoError: E11000 duplicate key error collection: docs.organisations index: declinedInvites_1 dup key: { : undefined }

我不明白这里发生了什么,为什么当我在架构中没有设置 unique 键时它会引发重复键错误?

【问题讨论】:

  • 嗨,我认为这应该解决你的问题click here
  • 你能显示docs.organisations 索引吗?
  • @GrégoryNEUT 就是这样,我不知道它会这样工作.. 这真的很烦人。感谢您的回答。
  • @Chrillewoodz 说句公道话,在看到你有趣的问题之前我并不知道它:D

标签: javascript node.js mongodb mongoose


【解决方案1】:

正如@Grégory NEUT 指出的那样,如果您在架构中设置了unique 键,然后将其删除,则索引仍然存在,将其视为您仍然拥有unique 键集。

解决方案是使用以下命令简单地删除索引:

db.items.dropIndex('some-index-path')

或者在我的情况下,我运行了我的自定义脚本来刷新数据库,它解决了所有可能出现问题的索引,而不是必须为每个索引运行它。

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多