3
votes

DeleteDocumentAsync and ReadDocumentAsync don't work for me when I have a partitioned collection. I used the RequestOptions:

await client.DeleteDocumentAsync(document.SelfLink, new RequestOptions { 
    PartitionKey = new PartitionKey("mykey")
}).ConfigureAwait(false); // This works.

var uri = UriFactory.CreateDocumentUri("db", "coll", "id1");

await client.DeleteDocumentAsync(uri, new RequestOptions { 
    PartitionKey = new PartitionKey("mykey")
}).ConfigureAwait(false); // This throws

Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document.

Any ideas?

1
Is myKey in new PartitionKey("mykey") the name of the PartitionKey attribute (e.g. lastName) or the value of the attribute (e.g. Smith)?Gaurav Mantri
Thanks for the help, but why will it succeed with the SelfLink with the wrong value?Jose
@Jose I can't delete documents with selfLink or uri if i set the wrong value the partition key.Would you please post your sample data structure? BTW, the class should be the RequestOptions not RequestOption in your code.Jay Gong
@Jose Hi,any progress?Does my answer helps you?Jay Gong
Sorry @Jay, I've been busy somewhere else. I'll try soon to rewrite a simple sample outside our code-base, which was written by somebody else.Jose

1 Answers

3
votes

I tried the your snippet of code which throws exception, it works for me.

I think you misunderstand the meaning of partitionkey property in the RequestOptions.

For example , my container is created like this:

enter image description here

The partition key is "name" for my collection here. You could check your collection's partition key.

And my documents as below :

{
    "id": "1",
    "name": "jay"
}

{
    "id": "2",
    "name": "jay2"
}

My partitionkey is 'name', so here I have two paritions : 'jay' and 'jay1'.

So, here you should set the partitionkey property to 'jay' or 'jay2',not 'name'.

var uri = UriFactory.CreateDocumentUri("db", "part", "1");

client.DeleteDocumentAsync(uri, new RequestOptions
{
    PartitionKey = new PartitionKey("jay")
}).ConfigureAwait(false); 

Hope it helps you.