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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With