I query a document and send it to my Angular application. Then I edit the model and send it back. My question is can you update a document from the model only? Document does not have any constructors, and ReplaceDocumentAsync requires Document? Or do I have to query the document first just to get same object i then I have to edit?
public void Put([FromBody] VesselView vessel)
{
var collectionLink = UriFactory.CreateDocumentCollectionUri("AssetControl", "Vessels");
_cosmosDocClient.ReplaceDocumentAsync(vessel);
}
Yes, you can. You only have to know the id
of document you want to replace. Tested with .Net client Microsoft.Azure.DocumentDB v2.2.1.
This can be achieved when passing the intended new document as JObject
. You can use any Json serializer for this. Also, note that your model does not have to contain the internal document fields like _rid
, _ts
, etc
Example code:
var client = new DocumentClient(url, key, policy);
var documentUri = UriFactory.CreateDocumentUri("myDB", "myCollection", "_replaceTest");
var newModel = JObject.Parse(@"{""id"": ""_replaceTest"", ""myData"": ""replaced!""}");
var response = await client.ReplaceDocumentAsync(documentUri, newModel);
var fromStorage = response.Resource;
Returned model is the full model containing the extra model fields:
{
"id": "_replaceTest",
"_rid": "FOOBAR==",
"_self": "dbs/FOO==/colls/BAR=/docs/FOOBAR==/",
"_ts": 1550491238,
"_etag": "\"0000e379-0000-0000-0000-5c6a9e660000\"",
"myData": "replaced!"
}
Obviously, as David Makogon hinted, you cannot rely on _etag
check this way, unless you include this property in the model you send to DocumentDB.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加