[Solved] E11000 duplicate key error collection

Hey All! I am getting the following error in a loop inserting multiple documents. I have never seen this before, and can’t seem to get it sorted.

Exception while invoking method ‘’ BulkWriteError: E11000 duplicate key error collection: meteor. index: id dup key: { _id: “kB2kLMmpt66zxhJaf” }

Here is the jist of my loop:

let ogInspection = Inspections.findOne({_id:inspectionId});
delete ogInspection._id;
const dates = [array of JavaScript Dates....roughly about 50 or so];

for(i = 0; i < dates.length; i++){
      const date = dates[i];
      const newInspection = Inspections.insert(ogInspection);
}

Is there some way around it throwing this error?

Thanks all!

I’m guessing a little bit here, but if you have any code (e.g., collection2/collectionHooks) that modifies an insert document with an _id, then the second insert in your loop will fail - can you log ogInspection._id after the insert?

If that is the problem, you could either delete the _id after the insert, or use Inspections.insert({ ...ogInspection });

2 Likes

I have tried logging the ogInspection and if I log it after the;

delete ogInspection._id;

call then it logs no _id. I would assume that because ogInspection is not in the loop, that it should be good…

How about this?

for(i = 0; i < dates.length; i++){
      const date = dates[i];
      const newInspection = Inspections.insert(ogInspection);
      console.log(ogInspection._id);
}
2 Likes

I was able to fix it…but the solution seems SUPER janky. I am hoping maybe someone has a better/cleaner way of doing this:

for(i = 0; i < dates.length; i++){
      const date = dates[i];
      const newId = new Mongo.ObjectID()._str;
      const inspectionData = {...ogInspection, _id: newId};
      Inspections.rawCollection().insertOne(inspectionData);
}

It seems for some reason when you try and loop through an array and do multiple inserts on the server without interaction from the client…it will always throw that error. I have been using Meteor for some time, and I guess I never knew this…

Going to leave this open in hopes someone has a cleaner solution…as I kinda hate this one…

I dont think it can be “meteor” generic - I’ve not come across this before and I frequently do inserts on the server side without client interaction - somewhere, something must be applying an _id on collection.insert().

This (or something like it) might help you track down what is applying the _id.

      const newInspection = Inspections.insert({
        set _id() { console.log(new Error("something applied an _id to me");},
        ...ogInspection
     );
2 Likes

Yeah not sure…been trying to track it down, but I am not seeing anything that is applying an _id. It always inserted the first document I asked it too in the array; however, I always get the error when it jumps to the next document. I actually have nested loops further down in the function and was even getting the same error on those after I fixed the initial array.

(I explored an idea what the initial problem may have been, but it was wrong, explanation deleted.)

1 Like

Well…yep! Turns out you were right! I went back and cleaned up my code more and found that I was removing the _id. Was doing it in a separate function that I imported from another file and then passed back. Serves me right for trying to be too fancy :joy:

Thanks for all the help…marking this one solved!

1 Like

So just for a final clarification, did you indeed have a field id along with a unique index which caused the problem?

1 Like

Actually nope…I thought I had this solved, but I am running into it again. I am looking into if perhaps it is because my array of elements came from a .fetch() array. I am wondering if maybe this is the cause…?

@peterfkruger Okay yes…this was the issue. Consider the following:

const fooArr = Collection.find({someparams}).fetch();
if(fooArr && fooArr.length >= 1){
  for(i = 0; i < fooArr.length; i++){
     //do some stuff then insert
  };
}

This will fail and throw the initial error; however, the following will not:

const fooArr = Collection.find({someparams})
fooArr.forEach((item) => {
  //do some stuff then insert
});

The issue seems to be with the cursor’s that are returned in the initial request. When using .fetch() this is when the errors fire. It sort of makes sense…but is a bit frustrating.