The db document is not working

I recently realized that DocumentDB supports standalone update operations through ReplaceDocumentAsync.

I replaced the Upsert operation below with a Replace operation.

var result = _client .UpsertDocumentAsync(_collectionUri, docObject) .Result; 

So now:

 var result = _client .ReplaceDocumentAsnyc(_collectionUri, docObject) .Result; 

However, now I get an exception:

Microsoft.Azure.Documents.BadRequestException: ResourceType document is unexpected. ActivityId: b1b2fd71-3029-4d0d-bd5d-87d8d0a2fc95

I don’t know why, upsert and replace have the same key, and the object is the same as upsert, so I expect it to work without problems.

All help was appreciated.

thanks

Update: We tried to implement this using the SelfLink approach, and it works for Replace, but self-loading does not work with Upsert. The behavior is rather confusing. I do not like that I need to create my own link in the code using string concatenation.

+5
source share
3 answers

I am afraid that creating selflink with string concatenation is your only option because ReplaceDocument(...) requires a link to the document. You show the collection link in your example. He will not stand the identifier and will not find the document as you wish.

The NPM module, documentdb-utils , has library functions to build these links, but it just uses string concatenation. I saw the equivalent library for .NET, but I can’t remember where. Perhaps this was the example of Azure or even in the SDK.

+3
source

You can create a document link for replacement using the UriFactory helper class:

 var result = _client .ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, docObject.Id), docObject) .Result; 

Unfortunately, this is not very intuitive, as Larry has already pointed out, but the replacement expects the document to already be there, and upsert is what it says about the gesture. I would say two different use cases.

+2
source

To update a document, you need to provide a collection of Uri. If you provide a Uri document, it returns the following:

The ResourceType document is unexpected.

Perhaps _collectionUri is a Uri document, the assignment should look like this:

 _collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName); 
+1
source

All Articles