Azure Cosmos DB SQL API UPDATE statement - don't replace whole document

Scotty H picture Scotty H · May 15, 2019 · Viewed 11.1k times · Source

Can I write an UPDATE statement for Azure Cosmos DB? The SQL API supports queries, but what about updates?

In particular, I am looking to update documents without having to retrieve the whole document. I have the ID for the document and I know the exact path I want to update within the document. For example, say that my document is

{
  "id": "9e576f8b-146f-4e7f-a68f-14ef816e0e50",
  "name": "Fido",
  "weight": 35,
  "breed": "pomeranian",
  "diet": {
    "scoops": 3,
    "timesPerDay": 2
  }
}

and I want to update diet.timesPerDay to 1 where the ID is "9e576f8b-146f-4e7f-a68f-14ef816e0e50". Can I do that using the Azure SQL API without completely replacing the document?

Answer

Nick Chapsas picture Nick Chapsas · May 15, 2019

The Cosmos DB SQL language only supports the Select statement.

Partially updating a document isn't supported via the sql language or the SQL API.

People have made it clear that that's a feature they want so there is a highly upvoted request for it that can be found here: https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/6693091-be-able-to-do-partial-updates-on-document

Microsoft has already started to work on that so the only thing you can do is wait.