Testing findOneAndUpdate upsert duplicate prevention Testing findOneAndUpdate upsert duplicate prevention mongoose mongoose

Testing findOneAndUpdate upsert duplicate prevention


Document Level Concurrency

WiredTiger uses document-level concurrency control for write operations. As a result, multiple clients can modify different documents of a collection at the same time.

For most read and write operations, WiredTiger uses optimistic concurrency control. WiredTiger uses only intent locks at the global, database and collection levels. When the storage engine detects conflicts between two operations, one will incur a write conflict causing MongoDB to transparently retry that operation.

Some global operations, typically short-lived operations involving multiple databases, still require a global “instance-wide” lock. Some other operations, such as dropping a collection, still require an exclusive database lock.

Document Level Concurrency

And in a single collection same _id will never exist _id has a unique index on it, so even if the situation you are explaining occurs, the first query which will get executed will create a new document and the later query will update it.