0

I have some records and am trying to insert_many using the following with pymongo:

db.collection.insert_many(records, ordered=False)

I know from this answer that setting ordered=False should allow me to skip duplicates, and I know from this answer that if there are duplicates within the list itself that it will throw the error. But there are no duplicates within the list being bulk written, and I'm still getting E11000 duplicate key error collection. Am I doing something else wrong?

snapcrack
  • 1,761
  • 3
  • 20
  • 40
  • Are you using pymongo? What version? When you say that you are "getting" that message, what do you mean? What's the full response from the database for the operation? – user20042973 Aug 16 '23 at 22:10
  • see my answer here https://stackoverflow.com/questions/76395778/mongodb-insertmany-skip-the-same-id-field-to-avoid-code-11000/76396057#76396057 with example how it works (the behavior will be the same for all drivers and in the shell). `But there are no duplicates within the list being bulk written` - the duplicated key should be mentioned in the error message – dododo Aug 17 '23 at 04:47

1 Answers1

2

You will get a E11000 duplicate key error if either:

  • A record you are trying to add has the same _id field as an existing record in the database; or
  • You have added a unique index and a record you are trying to add has matching unique index fields with an existing record

Even if your records are themselves all different, you may still get this error if there are exiting records in the database.

When pymongo throws the error, you will get some additional details. If you can add the full stack trace to your question then we might be able to help more.

Belly Buster
  • 8,224
  • 2
  • 7
  • 20
  • It's because there are records with the same `_id` field. But I thought `ordered=False` was supposed to deal with this? – snapcrack Aug 17 '23 at 00:37
  • Not quite, `ordered=False` means that MongoDB will attempt to load the remaining records rather than just stop on the first error. https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/#behaviors – Belly Buster Aug 18 '23 at 21:07