Search code examples
databasemongodbazure-cosmosdboptimistic-concurrency

Cosmos DB optimistic concurrency using MongoDB API


We are using Cosmos DB via the Mongo API in a .net Core environment. Multiple clients can modify a single collection and we decided to use the optimistic concurrency model.

From the docs here I know there is a build in ETAG field in a cosmos DB. The problem is that I have no idea how to retrieve this field using the MongoDB API. I am not even sure if I can access this field with this API.

The alternative would be to implement a unique id field on my own but I'd rather prefer to use something in-build.


Solution

  • Thanks for your feedback. The MongoDB protocol/API itself does not offer support for optimistic concurrency (see MongoDB Optimistic Concurrency Control With .NET). CosmosDB’s API for MongoDB presents the MongoDB protocol/API so shares this limitation.

    For many scenarios related to optimistic concurrency, MongoDB’s update command takes care of it, as it can atomically update a portion of a document (covered at the link above). The MongoDB API for CosmosDB supports the update command in this way.

    To take advantage of the ‘etag’ feature directly, you will need to use another one of the Cosmos DB APIs that supports it.

    Hope it helps.