简体   繁体   中英

Read before Upsert in Azure Cosmos DB

i am confused how Azure Cosmos DB "UpsertDocumentAsync" C# API works. Looks that the object is updated if you first read it:

var response = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, "docId"), new RequestOptions { PartitionKey = new PartitionKey("pk") });
var upsertOrder = response.Resource;
var upsertOrder = new Measurements { Id = "docId" , value = 3243};
upsertOrder.SetPropertyValue("value", 5678);
response = await client.UpsertDocumentAsync(collectionLink, upsertOrder);

If instead i directly create an object:

 var upsertOrder = new Measurements { Id = "docId" , value = 3243};
 response = await client.UpsertDocumentAsync(collectionLink, upsertOrder);

this create a new object!! Am i really constrained to read the doc before patching it??

EDIT I understand i need to add the partitionKey too. So it does not create a new object anymore BUT it sets all the non-passed field to NULL. This is not a patch behavior! So am i correct i need to pass all fields anyway?

thanks lot to shade lights here.

https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/6693091-be-able-to-do-partial-updates-on-document

As you can see the feature is planned but only got feedback on Mar 5, 2018 so it may be a while until delivery.

For the moment you can either read the document, update it, then insert as you have stated - part of the reason document size is an important design choice, or you could also write a stored procedure to do this and use this do partial updates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM