Chrillewoodz
Chrillewoodz

Reputation: 28318

MongoDB - Trying to save multiple documents results in duplicate key error for a schema's ObjectId despite no unique key set

I got these two schemas:

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}
}));

If I save one Organisation to the database, and then try to save another using this route:

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);
    }
  });
});

It throws an error like:

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

The same goes for the empty array declinedInvites, it will throw an error saying the same thing but:

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

I don't understand what is happening here, why does it throw duplicate key error when I have no unique key set in the schema(s)?

Upvotes: 2

Views: 87

Answers (1)

Chrillewoodz
Chrillewoodz

Reputation: 28318

As @Grégory NEUT pointed out, if you go from having a unique key set in your schema and then remove it, the index will still be there, treating it as if you still have the unique key set.

The solution to this is to simply remove the index(es) with this command:

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

Or in my case I ran my custom script for refreshing the database, which solved it for all indexes where this might have been a problem, rather than having to run it for each index.

Upvotes: 1

Related Questions