1

Error description

I write a node.js application with TypeScript using MongoDB (npm mongodb).

On writing an array of objs with

await collection.insertMany(objs)

to a collection the error

E11000 duplicate key error collection: CollectionName index: _id_ dup key {_id: ObjectId('60c5d8170f845073 5882376a')}"

is thrown. No error is thrown on iterating over all objects and inserting them one by one:

for (const obj of objs) {
    await collection.insertOne(obj)
}

I have not specified any indexes manually therefore the default index generated by MongoDB should be used. The collection is empty/non existent on insertMany-call.

What I have tried

  1. I have tried deleting all _id-fields before writing to the database.

     for (const obj of objs) {
         delete obj["_id"] 
     }
     await collection.insertMany(objs)
    
  2. I have dropped the DB with MongoCompass

Question How is it possible that this error is thrown?

pinguiny
  • 71
  • 1
  • 6

0 Answers0