pymongo.errors.BulkWriteError: batch op errors occurred MongoDB Origin: Data OSI layer 7 · Application Last updated 5 Nov 2026

PyMongo: pymongo.errors.BulkWriteError: batch op errors occurred

A bulk insert/update had one or more failed operations; details are in e.details.

Exact message you may see pymongo.errors.BulkWriteError: batch op errors occurredBulkWriteError: batch op errors occurredpymongo BulkWriteError E11000 duplicate keyMongoBulkWriteError: E11000 duplicate key error collection
Seen on: MongoDB Python

Meaning

Catch the exception and inspect e.details['writeErrors'] for index, code and message (e.g. 11000 duplicate). Use ordered=False to continue after errors, and handle duplicates deliberately (upsert, $setOnInsert).

Common causes

  • Duplicate keys in insert_many
  • Validation failures
  • Unique index violations
  • Writes exceeding size limits
  • Permission errors

⚡ Quick fix

  1. try: ... except BulkWriteError as e: print(e.details["writeErrors"])
  2. Use ordered=False
  3. Replace inserts with upserts where appropriate

Detailed fix by platform

Python

  1. coll.insert_many(docs, ordered=False) # continue past duplicates

How to diagnose

  1. details — writeErrors?
  2. Code — 11000?
  3. Ordered — False?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

Atlas: bad auth: Authentication failed — MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password) (MongoDB)Atlas: bad auth: Auth…BSONObj size is invalid / exceeds maximum document size (16MB) — MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB) (MongoDB)BSONObj size is inval…Document failed validation — MongoDB: MongoServerError: Document failed validation (code 121) (MongoDB)Document failed valid…E11000 duplicate key — MongoServerError: E11000 duplicate key error collection: db.users index: email_1 dup key (Node.js)E11000 duplicate keyNotImplementedError: Collection objects do not implement truth value testing — PyMongo: NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None (MongoDB)NotImplementedError: …pymongo.errors.ServerSelectionTimeoutError — PyMongo: pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s (MongoDB)pymongo.errors.Server…Sort/Group exceeded memory limit of 100 MB (allowDiskUse) — MongoServerError: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in (MongoDB)Sort/Group exceeded m…pymongo.e…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.