Skip to content

MongoDB Index Creation Error Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about MongoDB Index Creation Error Fix. We cover key concepts, practical examples, and best practices.

MongoDB index creation can fail with duplicate key errors when existing documents violate the uniqueness constraint, or when background index builds time out on large collections. These errors prevent schema migration and degrade query performance.

The Wrong Way

from pymongo import MongoClient

client = MongoClient("mongodb://localhost:27017")
db = client["ecommerce"]
users = db["users"]

# Insert documents with duplicate emails
users.insert_many([
    {"name": "Alice", "email": "alice"@example".com"},
    {"name": "Bob", "email": "alice"@example".com"},  # Duplicate!
])

# Try to create a unique index
users.create_index("email", unique=True)

Output:

pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: ecommerce.users index: email_1 dup key: { email: "alice@example.com" }

The Right Way

Clean duplicate data before creating the unique index:

from pymongo import MongoClient, IndexModel

client = MongoClient("mongodb://localhost:27017")
db = client["ecommerce"]
users = db["users"]

# Find duplicates
pipeline = [
    {"$group": {"_id": "$email", "count": {"$sum": 1}},
    {"$match": {"count": {"$gt": 1}}
]

duplicates = list(users.aggregate(pipeline))
for dup in duplicates:
    email = dup["_id"]
    # Keep one, remove duplicates
    cursor = users.find({"email": email}).sort("_id", 1)
    first_id = cursor[0]["_id"]
    users.delete_many({"email": email, "_id": {"$ne": first_id}})

# Now create the unique index
users.create_index("email", unique=True)
print("Index created successfully")

Output:

Index created successfully

Step-by-Step Fix

1. Identify duplicate documents

pipeline = [
    {"$group": {"_id": "$field", "count": {"$sum": 1}},
    {"$match": {"count": {"$gt": 1}}
]
for doc in collection.aggregate(pipeline):
    print(f"Duplicate value: {doc['_id']} ({doc['count']} times)")

2. Use dropDups (older versions)

# MongoDB 3.x only - use with caution
collection.create_index("email", unique=True, dropDups=True)

3. Build indexes in background

collection.create_index("email", background=True)

4. Create compound indexes

index = IndexModel([("status", 1), ("created_at", -1)])
collection.create_indexes([index])

5. Handle index creation timeout

from pymongo.errors import OperationFailure

try:
    collection.create_index("email", unique=True)
except OperationFailure as e:
    if "already in use" in str(e):
        print("Index already exists")
    else:
        raise

Prevention Tips

  • Clean duplicate data before creating unique indexes.
  • Use create_indexes with IndexModel for bulk index creation.
  • Build indexes in the background on production systems (background=True).
  • Monitor index build progress with db.currentOp().
  • Plan indexes before inserting data to avoid cleanup work.

Common Mistakes with index error

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world MONGODB code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What does E11000 error mean?

E11000 is MongoDB's duplicate key error. It occurs when an insert or update would violate a unique index constraint. The error includes the collection name and the duplicate key value.

Can I create indexes without blocking reads?

Yes. Use background=True when creating indexes. Background builds allow read/write operations during construction but are slower than foreground builds.

How do I list existing indexes on a collection?

Use collection.index_information() to view all indexes. It returns a dict with index names as keys and index details as values.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro