MongoDB Command insert failed: E11000 duplicate key error collection

Today in this article we shall see how to resolve the error E11000 duplicate key error collection in the MongoDB command execution.

Today in this article, we will cover below aspects,

Issue Description

Mongo Insert/Update operation gives the below error,

MongoDB.Driver.MongoComamndException: Command insert failed: E11000 duplicate key error collection:[yourccollection] index :[key] dup key: { : }

Resolution

I had this error recently for the Upsert operation,

var returnDoc = await collectionnew.FindOneAndUpdateAsync<Library>(
                   filter: Builders<Library>.Filter.Eq("_id", userId),
                   update: update.Combine(updates),
                   options: new FindOneAndUpdateOptions<Library, Library>
                   {
                       IsUpsert = true,
                       ReturnDocument = ReturnDocument.After,
                   });
 
               if(returnDoc!=null)
               {
                   //your logic here
               }

  • E11000 duplicate key error could occur due to many factors depending on the type of operation you are performing.

  • This error however indicates that the issue has occurred because the key with which you performed the Mongo operation key already exists with the value specified.

  • This also meant the indexed key in the error is a unique key and not the non-unique key or sparsed key. That means only one key with a unique value can exist in the given mongo collection.

Generally, you will have the default _id as a unique key in the mongo collection.

Example:

E11000 duplicate key error collection

But you can add more unique keys depending on your requirements provided you are 100% sure, its value will remain unique for the whole collection and won’t conflict with other ids.

To fix the issue, please try any of the below guidelines,

  • Do you need the indexed field to be unique really? If not, then create an index with the non-unique index or sparsed index, etc.

Example:

Command insert failed

Above, we have used a not checked “Create unique index” and created non-unique index since the Date field can have duplicates and ideally cannot remain unique logically.

If you are 100% sure of the index field will remain unique across the collection then only you can make it a unique indexed field else make it indexed but non-unique so that duplicated values can be allowed.

  • Delete unnecessary index – Don’t use indexed if not necessary.

For Example ,

Please visit ; MongoDB Indexing Guidelines and Best Practices

  • Apply a unique index to only those fields which will have values.

For example,

if the value is not specified then it’s possible mongo assigns the null value, then a null value will be assigned to the very first record but the second record will throw the duplicate error issue.

  • Please make sure the indexed fields are properly indexed.

Using this option also meant that you already have _id or any other unique id to be used for the query considering the performance.

References:

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “MongoDB E11000 duplicate key error collection

  1. Perfect! thanks it was super helpful. I had been trying to solve it for 4 hours straight and you helped me! thank you so much

Leave a Reply

Your email address will not be published. Required fields are marked *