Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentDB Replace not Working

I recently realized that DocumentDB supports stand alone update operations via ReplaceDocumentAsync.

I've replaced the Upsert operation below with the Replace operation.

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

So this is now:

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

However, now I get the exception:

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

No idea why, upsert and replace are of the same vein and the object is the same that worked for upsert, so I would expect it to work without problems.

All help appreciated.

Thanks

Update: Have tried to implement this using the SelfLink approach, and it works for Replace, but selflink does not work with Upsert. The behavior is quite confusing. I don't like that I have to build a self link in code using string concatenation.

like image 350
Tristan Rhodes Avatar asked Jun 09 '16 12:06

Tristan Rhodes


People also ask

Is DocumentDB the same as MongoDB?

Amazon DocumentDB is a NoSQL JSON document database service with a limited degree of compatibility with MongoDB. DocumentDB is not based on the MongoDB server. Rather it emulates the MongoDB API, and runs on top of Amazon's Aurora backend platform.

What is upsert in CosmosDB?

With an upsert operation we can either insert or update an existing record at the same time. To compare to the existing records, upsert statement use key column(s) to determine if this is a new or existing record. We will use Azure SQL database as a source and CosmosDB as a sink.


2 Answers

I'm afraid that building the selflink with string concatenation is your only option here because ReplaceDocument(...) requires a link to the document. You show a link to the collection in your example. It won't suck the id out and find the document as you might wish.

The NPM module, documentdb-utils, has library functions for building these links but it's just using string concatenation. I have seen an equivalent library for .NET but I can't remember where. Maybe it was in an Azure example or even in the SDK now.

like image 58
Larry Maccherone Avatar answered Sep 30 '22 21:09

Larry Maccherone


You can build a document link for a replace using the UriFactory helper class:

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

Unfortunately it's not very intuitive, as Larry has already pointed out, but a replace expects a document to already be there, while an upsert is what it says on the tin. Two different use-cases, I would say.

like image 26
Yannick Meeus Avatar answered Sep 30 '22 21:09

Yannick Meeus