Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 479
Scotty H Avatar asked May 15 '19 18:05

Scotty H


People also ask

What is Upsert in Cosmos DB?

Upserts a Document as an asychronous operation in the Azure Cosmos DB service. UpsertDocumentAsync(Uri, Object, RequestOptions, Boolean, CancellationToken) Upserts a document as an asynchronous operation in the Azure Cosmos DB service.

How do you reduce Rus in Cosmos DB?

The best way to optimize the RU cost of write operations is to rightsize your items and the number of properties that get indexed. Storing very large items in Azure Cosmos DB results in high RU charges and can be considered as an anti-pattern.

Which API should you use to store and query JSON documents in Azure Cosmos DB?

Quick note: Core (SQL) API is the native API in Cosmos DB and is also called SQL API. It supports a SQL-like query language that allows retrieving documents using SELECT and other basic SQL commands.


Video Answer


1 Answers

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.

like image 101
Nick Chapsas Avatar answered Oct 13 '22 21:10

Nick Chapsas