Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Sample Update - Confusion over PartitionKey when deleting #870

Open
euangordon opened this issue Sep 15, 2021 · 0 comments
Open

Code Sample Update - Confusion over PartitionKey when deleting #870

euangordon opened this issue Sep 15, 2021 · 0 comments

Comments

@euangordon
Copy link

euangordon commented Sep 15, 2021

I have been using Cosmos for some time and recently moved over to use Microsoft.Azure.Documents.Client. I found the documentation pretty easy to get going until I tried to delete documents.

https://github.com/Azure/azure-cosmos-dotnet-v2/blob/master/samples/code-samples/DocumentManagement/Program.cs

I realise that my mistake is somewhat silly, but possibly a couple of comments would have saved me a few hours.

In the sample for deletions:

private static async Task DeleteDocumentAsync()
        {
            Console.WriteLine("\n1.7 - Deleting a document");
            ResourceResponse<Document> response = await client.DeleteDocumentAsync(
                UriFactory.CreateDocumentUri(databaseName, collectionName, "SalesOrder3"),
                new RequestOptions { PartitionKey = new PartitionKey("Account1") });

            Console.WriteLine("Request charge of delete operation: {0}", response.RequestCharge);
            Console.WriteLine("StatusCode of operation: {0}", response.StatusCode);
        }

I wrongly interpretted the PartitionKey to be the PartitionKey set on the container, in my case VesselID, when it should have been the value of the VesselID, in my case an int like 123.

A couple of suggestions to make this more obvious.

  1. If the DeleteDocumentAsync method took two inputs, one for partitionKey and one for the documentID, it would be more obvious. The calling code would show the paritionKey which would have made it a little more easy to follow
  2. A comment to say that it was the value of the PartitionKey.

In my own code, I ended up with, it could obviously be made more generic:

private static async Task DeleteDocumentAsync(int partitionKey, string documentID)
        {
            Console.WriteLine("\n1.7 - Deleting a document");
            ResourceResponse<Document> response = await client.DeleteDocumentAsync(
                UriFactory.CreateDocumentUri(DatabaseID, CollectionName, documentID),
                new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });

            Console.WriteLine("Request charge of delete operation: {0}", response.RequestCharge);
            Console.WriteLine("StatusCode of operation: {0}", response.StatusCode);
        }

@euangordon euangordon changed the title Code Sample Update Code Sample Update - Confusion over PartitionKey when deleting Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant