Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentDB: What's the point of "Upsert"?

I'm trying to understand this part of the API so I can update documents in the most efficient way possible.

Given the following:

  • "Replace" requires that a document already exists
  • "Upsert" doesn't require that a document exists, but needs the document ID if it's going to do an update.
  • Neither command can do partial document updates, which means I can't think of any scenario where I wouldn't have to query the document first, then make the change to the affected property, then replace/upsert the entire document.

If I always have to query the document first anyway so as to avoid wiping out any property values that aren't passed back to the upsert/replace, and I can't do a partial updates, what's the point of having both upsert and replace?

Am I missing the intended use cases for these two commands?

like image 462
Simon Ordo Avatar asked Jul 26 '17 21:07

Simon Ordo


People also ask

What is Upsert in CosmosDB?

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.

Should I use CosmosDB?

It is a good choice for any serverless application that needs low order-of-millisecond response times, and needs to scale rapidly and globally.

Is Cosmos DB a DocumentDB?

Azure Cosmos DB is the next big leap in globally distributed, at scale, cloud databases. As a DocumentDB customer, you now have access to the new breakthrough system and capabilities offered by Azure Cosmos DB.

Can Azure functions access DocumentDB?

There are two ways you can connect to documentDB from an Azure function. DocumentClient documentClient = new DocumentClient( "SERVICE_ENDPOINT", "MASTER_KEY", ConnectionPolicy. GetDefault(), ConsistencyLevel. Session);


1 Answers

You've already described the key differences between the two. Upsert will create a document if it doesn't already exist otherwise overwrite it. Replace requires that a document already exist and then overwrites it. Which to use is a concern of your application. There are certain circumstances where you would want to use replace because if the document didn't already exist it would constitute an error in your business logic. Otherwise they are very similar.

I understand that the lack of being able to do a partial update can appear frustrating. However, Cosmos has a powerful server side programming model in the form of Stored Procedures which you write in Javascript. You could easily create a SPROC that receives a partial document and updates or adds only those properties that are new or changed which would give you the functionality you're ultimately looking for.

like image 89
Jesse Carter Avatar answered Sep 20 '22 08:09

Jesse Carter