Duplicate key error in Mongoose' findOneAndUpdate and upsert Duplicate key error in Mongoose' findOneAndUpdate and upsert mongoose mongoose

Duplicate key error in Mongoose' findOneAndUpdate and upsert


This post suggests that searching for and saving a non-existing document are not atomic, which means between the searching and the saving, another query could have yielded a not found result for the same search criteria. The only solution then, it seems, is to ensure that if duplicate key errors are handled, for example, by reapplying MongoDB operations to the document that was thrown out.

I would follow this solution for now. If anyone has as different method of handling this, please let me know. Thanks!