2

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.

Curious Learner
  • 1,182
  • 1
  • 4
  • 17
  • Please post code, not images of code. As for your problem: You don't show the index definiton on `data` so nobody knows, how this index looks like. But it problably forbids two objects with having an empty data array or a missing data array ... – derpirscher Apr 08 '23 at 06:42
  • And creating a lot of objects and only adding a value that should be unique later on may be a bad idea, because you may end up in a situation, where you cannot assign a value to an already existing element because of an index violation ... You may want to rethink that design ... – derpirscher Apr 08 '23 at 06:48
  • added the index of the collection to the question now. Please check once – Curious Learner Apr 08 '23 at 06:53
  • It seems like your index is not on the elements in the `data` array but on the property `data.email` – derpirscher Apr 08 '23 at 07:00
  • Does this answer your question? [MongoDB: Unique index on array element's property](https://stackoverflow.com/questions/6743849/mongodb-unique-index-on-array-elements-property) – derpirscher Apr 08 '23 at 07:00
  • Problem is happen when creating a document... not when pushing to the array. When I am creating the document without the `data:[]`, still the data filed is getting created. I don't want data to be created – Curious Learner Apr 08 '23 at 07:28
  • Yeah, but probably only on the second element, because there is already an element with `data.email === undefined` existing. And still, even if the index was working correctly (ie allowing elements with `data === []` you can create three elements with `data =[]` and then when inserting the email, you can run into problems and have elements in you database where you cannot set the email. What to do with such elements? – derpirscher Apr 08 '23 at 08:52

2 Answers2

2

Try

  1. removing the index:true or
  2. removing the unique:true fields from the Schema (rely on different method of validation of duplicate items)
1

Your ORM, Mongoose, creates an empty array when you save a new instance. Mongo considers undefined/null a unique value so you can't have two instances that have an empty data array as that would cause data.email to be undefined/null in the second document as well.

Setting data to be undefined, rather than [] should remedy that.

As mentioned by vkarpov15 on Sequelize's issue tracker:

If you have an array in your schema, mongoose will create an empty array [] for you by default so you can do .push() and such without explicitly creating the array. To shut this off, overwrite the default array default

var schema = new Schema({
 myArr: { type: [String], default: undefined }
});

As you posted your code as an image and not text you can integrate the above solution into your code yourself, as I'm not going to transcribe it. Be mindful of people trying to help you as the guidelines on how to ask questions aren't for fun but proper manners.

RickN
  • 12,537
  • 4
  • 24
  • 28