A few months ago, I took on the responsibility of managing a non-relational DocumentDB database, which has since been updated to CosmosDB, hosted on Azure. During this journey, I encountered challenges, particularly when it came to deleting specific records. It became evident that understanding how partitions work and selecting the correct partition by ID played a pivotal role in overcoming these obstacles.
To facilitate this process, I developed a Windows console application sample in C#. This application serves to collect document IDs based on a query and subsequently deletes the documents associated with those IDs. This tool has proven to be invaluable in efficiently managing and maintaining our database, streamlining the deletion process for optimal database performance.
Initially we install the libraries Microsoft.Azure.DocumentDB and Newtonsoft.Json.
To establish a connection between the DocumentDB database and the Windows console application, you will require the necessary credentials, including the URI and primary key. These credentials can be found in the “keys” tab within the Azure portal.
Next, you’ll need to initialize the DocumentDB URI, primary key, as well as specify the collection and database names.
private const string connectionEndpointUri ="https://dropdocuments.documents.azure.com:443/";
private const string primaryKey = "Kl9XbLDeSPjcmoJra7tKkHblfamvhmxFdzcw7LN3Pl44ENCdBYHax6krgmqGgqfpRbUEj73Rml0YAOR2ALJBZg==";
private static string databaseId = @"DatabaseName";
private static string collectionId = @"CollectionName";
private Uri collectionLink = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
Read More »