Home > Software engineering >  Mongoose error "E11000 duplicate key error collection" while inserting multiple values
Mongoose error "E11000 duplicate key error collection" while inserting multiple values

Time:10-12

I'm trying to import some data into MongoDB and keep running into this error.

MongoBulkWriteError: E11000 duplicate key error collection: MyDB.clist_list index: clients.email_1 dup key: { clients.email: null }

Model:

interface Client {
  name: string;
  email: string;
}

interface Agent {
  pipedrive_id: number;
  name: string;
  clients?: Array<Client>;
}

const clientSchema = new Schema<Client>({
  name: { type: String, required: true },
  email: {
    type: String,
    trim: true,
    lowercase: true,
    unique: true,
    validate: {
      validator(str: string) {
        return /^\w ([\\.-]?\w )*@\w ([\\.-]?\w )*(\.\w{2,3}) $/.test(str);
      },
      message: 'Please enter a valid email',
    },
    required: [true, 'Email required'],
  },  
});

const agentSchema = new Schema<Agent>({
  pipedrive_id: { type: Number, required: true },
  name: String,
  clients: [clientSchema],
});

const AgentClients = model<Agent>('client_list', agentSchema);

As you can see on the Model, the clients collection is optional. Also each run the DB is removed (deleted) and created completely now (just for testing purposes, so that there are no left over indexes or whatnot).

And here's the payload.

[
  {
    first_name: 'Jen Test',
    pipedrive_id: 2186,
    clients: []
  },
  {
    name: 'Carl Test',
    pipedrive_id: 2191,
    clients: []
  }
]

This is the part that confuses me. How can email have a duplicate value of null when there are no Clients ???

I'm using the insertMany method on the Model to insert the data (if that is of any help).

I know there are tons of similar Questions here on this subject but none have my specific issue.

CodePudding user response:

You specified unique: true on the email property in the clientSchema. And since both of your inputs does not have email specified, MongoDB will assume it's null.

So you have a conflict there. On one side you specified that email has to be unique, and on the other side you are trying to insert 2 documents with the same email value of null.

If you want to keep the email field unique, and in the same time to allow null values, you should check for Sparse Indexes.

In your schema, you should pass additional property sparse: true.

  • Related