MongoDB - 尽管没有唯一的密钥集,但尝试保存多个文档会导致模式的ObjectId出现重复的密钥错误

时间:2017-11-20 09:06:15

标签: javascript node.js mongodb mongoose

我有两个架构:

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密钥时会出现重复密钥错误?

1 个答案:

答案 0 :(得分:0)

正如@GrégoryNEUT指出的那样,如果您从模式中设置了unique密钥然后将其删除,那么索引仍将存在,将其视为仍然具有{{1}键集。

解决方法是使用以下命令删除索引:

unique

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