Search code examples
mongodbdistributed

Duplicate key error in Mongo Sharded Cluster


I'm running into duplicate key error in Mongo Sharded Cluster(MSC) which does not happen when working with Mongo Replica Set.

I have simple operation in client app:

// pseudocode - email is unique key
subscriber = db.newsletter.find({email: "[email protected]"})
if (subscriber == null) {
  db.newsletter.insert({email: "[email protected]", name: "John"})
} else {
  db.newsletter.update({email: "[email protected]"}, {name: "John"})
}

I did not include it in above code but I need to retrieve _id of a document so using upsert is not an option.

Error: If above process is executed twice for the same and new email it would cause duplicate key error. Second iteration will not be able to find subscriber therefore it will attempt to insert despite document already existing in collection.

As far as I know, having single instance of above client running should not cause duplicate key error, but it does.


Solution

  • This would be the job of the findAndModify command, or the findOneAndUpdate method.

    There is an upsert flag, which will create the document if it doesn't exist. You would also have to use the new flag, to return the new document if it is created.