Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDocumentClient.UpsertDocumentAsync doesn't update, it inserts duplicated ids

I have a VS2017 solution in C#, and I'm using IDocumentClient.UpsertDocumentAsync to Upsert some documents into my cosmosdb documentdb collection. But I noticed it's actually creating new documents with the same id while there already is a document in the collection with that id.

Now after upserting a new document with the same id the query result looks something like this:

select * from c where c.id = "aaaa-bbbb-cccc"

[
    {
        "id": "aaaa-bbbb-cccc",
        "firstname": "john",
        "lastname": "doe"
    },
    {
        "id": "aaaa-bbbb-cccc",
        "firstname": "john",
        "lastname": "doe",
        "age": "35"
    }
]

I'm quite confused with this behavior; maybe I don't properly understand the definition of "upsert". Would appreciate if anyone can clarify this for me.

like image 527
DeveloperInToronto Avatar asked Feb 13 '18 21:02

DeveloperInToronto


1 Answers

In Cosmos DB, the "id" is not a unique value. Instead it is the combination of partition key (in your case "age") and "id" that is unique. Cosmos DB allows the partition key to be absent (since it is lenient with schema) and treats missing partition key values as a special value (= "undefined"). Partition key values are also immutable - to change these, you need to delete/insert.

So in this case, you have two documents, one with [age="35", "aaaa-bbbb-cccc"] and another with [age=undefined, "aaaa-bbbb-cccc"] created by the upsert call. If you changed the upsert call to a replace, you would have received a NotFound error.

like image 136
Aravind Krishna R. Avatar answered Sep 25 '22 15:09

Aravind Krishna R.