node.js – MongoDB error E11000 duplicate key error collection due to empty array containing document with email null

This is the code I am writing:

      const createOrgPerso= new Person({
        org: newOrganisation._doc.id,
        // data: [],
      });

But the empty array is still getting created. And then this code is run again, it gives E 11000 duplicate key error collection: managerhq.studentdatas index: data.email_l dup key: { data.email: null } error

enter image description here

Problem description :

I think this is because I have set required: true for some fields.

What I want is:

Just create a document with the org field only. I will fill the data array with documents later.
My goal is: same email(also few other fields) cannot be repeated in the data array

This is the mongoose schema

enter image description here

The indexes in that collection
enter image description here

If any more details is required, please tell in comments.

Read more here: Source link