• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:9 mins read
You are currently viewing E11000 duplicate key error index in MongoDB

In this MongoDB article, we will discuss what the E11000 duplicate key error index is, how it may arise, and how to resolve it. The E11000 duplicate key error index is an error that occurs when we try to insert a document into a MongoDB collection with a unique index, and the index already contains a document with the same value for the unique key.

Advertisements

1. Handling the E11000 Duplicate Key Index Error in MongoDB?

There are several ways to fix the E11000 Duplicate Key Index Error in MongoDB, let’s see examples. Assume that we have the collection student with the following documents. Note that the document with _id 3 and 4 contains the same values for the name and email fields.


#Create Collection
db.student.insertMany([
       {
         "_id": 1,
          "name": "Messy",
          "email": "[email protected]"
       },
       {
           "_id": 2,
          "name": "Jessie",
          "email": "[email protected]"
       },
       {
          "_id": 3,
          "name": "Lucy",
          "email": "[email protected]"
       },
       {
          "_id": 4,
          "name": "Lucy",
          "email": "[email protected]"
       }
    ]
 )

1.1 Remove the Duplicate values from the documents

You would be required to remove the duplicate values from the collection when you need to create an index. If you have duplicate values on the indexing column, you will get E11000 duplicate key index error

Firstly, let’s create a unique index of the collection student by using the createIndex() method.


# Usage of createIndex() method
db.student.createIndex( { "name": 1 }, { unique: true } )

The output generates the E11000 duplicate key index error because the name field contains the duplicate name Lucy.

 E11000 duplicate error MongoDB

As we have identified the duplicate document, we can drop the collection by using the drop query. This will allow us to insert the new documents with the same unique index field value. Alternatively, you can also drop the specific document that is causing the issue. But if you have millions of records, it would be challenging to identify the duplicate records.


#Usage of drop query
Db.student.drop()
 E11000 error in MongoDB

#Usage of insertMany() method
db.student.insertMany([
       {
         "_id": 1,
          "name": "Messy",
          "email": "[email protected]"
       },
       {
           "_id": 2,
          "name": "Jessie",
          "email": "[email protected]"
       },
       {
          "_id": 3,
          "name": "Lucy",
          "email": "[email protected]"
       },
       {
          "_id": 4,
          "name": "Marry",
          "email": "[email protected]"
       }
    ]
 )

Again, we have attempted to create the name field as a unique index of the collection student by using the aforementioned createIndex() query. Now that the name field is the unique index here.

1.2 Update the existing document

Sometimes you also get this error E11000 duplicate key error index in MongoDB, when you are trying to insert the same records that are already part of the index.

To fix this error, update the existing document instead of inserting a new one. Let’s insert the existing record and see if we get E11000 duplicate key error.


# Insert existing record
db.student.insertOne( { name: "Marry", email: "[email protected]" } )

However, it throws the E11000 error as shown below because the name field already exists in the document with the same value.

 E11000 error in MongoDB

We can fix this by using the update query. In this case, the update operation will only insert a document if a document with the same name value does not already exist in the collection.

The example below updates the name Lucy with a different email. The upsert option in the query updates the existing email, if it already exists, or inserts a new email if it doesn’t exist.


#Usage of updateOne() method
db.student.updateOne(
   { name: "Lucy" },
   { $set: { email: "[email protected]" } },
   { upsert: true }
)

The output shows that the document to be updated is matched and modified.

1.3 Insert unique index key value

Therefore, we can also avoid the E11000 duplicate key error index by inserting the new document with the unique index value. Consider the following case where we have used the insertOne() method to insert the document that has a unique name field value but the email address is duplicate.


# Usage of insertOne() method
db.student.insertOne( { name: "Marrie", email: "[email protected]" } )

The output displayed that the document is inserted in the collection as the name index key has a different value.

 E11000 error in MongoDB

1.4 Dropping and recreating a unique index to remove E11000 error in MongoDB

Alternatively, if we need to maintain the duplicate value in the indexed field, we can modify the index to allow for duplicate values. We can do this by removing the unique index constraint from the field, or by creating a non-unique index instead.

To remove the unique index constraint, we can use the dropIndex() method to drop the index. In the following query, we have dropped the name index from the collection. 


#Usage of dropIndex() method
db.student.dropIndex("name_1");

Then recreate the index without the unique option.


db.student.createIndex({ name: 1 });
 E11000 error in MongoDB

2. Conclusion

In conclusion, we can use any of the above solutions to fix the E11000 duplicate key error in MongoDB. Further, it is also important to take appropriate backups of our database before making any changes.

More details about this topic can be found here.