6 Mar

CRUD Operations Using Azure Cosmos DB


When I am learning something new, I start from the genuine basics and build up from there. Azure Cosmos DB is no different and as someone just starting out with Azure, I thought it best to start with one of the most fundamental of services: CRUD operations on data. Admittedly, I may write bad code along the way, because I am just trying to get things to work, not developing a long-lived application.

First, there is no need for me to repeat the information available in some basic Microsoft documentation to get you all set up and ready to go, so I will assume you have done the following. Second, I’ll be leaving out irrelevant code that isn’t part of working with the DB.

  1. Secured an Azure account (free for 1 month or $200 at the time of this writing)
  2. Created a Cosmos DB using the SQL (DcoumentDB) API.
  3. Install Microsoft.Azure.DocumentDB into your Visual Studio solution with NuGet.
  4. Obtained your endpoint URL and primary key from the Azure Cosmos DB dashboard. This is found under the key tab on the left of the Cosmos DB admin page.

Below is the simple entity I am using to work with the Cosmos DB.


Now that I have a tidy entity to work with, let’s get some of them into the database.

First, let’s make sure there is a DB there to work with, along with a collection. This is the first thing my command line application does. I am going to admit something here, my call to this method is appended by .Wait() which does exactly what it sounds like. It ensures the async operations in my method are not just awaited within the method. They must come back to move forward. To be clear, this is a hack and an example of learning code. I warned you.


With a database and a collection into which we will store our documents, now it’s time to add some sensor data. I’m using a very simple SensorTickerFactory that just generates random values for the property values of the entities it creates.


Great! We have some data in our Cosmos DB. When I look at it through the admin portal Data Explorer tool (again, just a tab on the left), I see this:

I will leave the addition of the underscore values to our JSON to you, dear reader, so we can keep on task here.

The next step is to modify pre-existing documents.


Awesome! We’ve now updated our documents.

Let’s kill them! The next step is to simply get all the documents in the DB and delete them.


And there you have it. Basic CRUD operations.

I did some funky things because this code is all Async, but in a real application, we can see why these operations would be Async. Often, you want your UI to continue living its life and responding accordingly which you are performing a data operation.

3 thoughts on “CRUD Operations Using Azure Cosmos DB

  1. Are the poco’s JsonProperty attributes required? Also is overriding ToString required?

  2. The JSON properties on the POCO are not necessary. I added them because when the POCO is serialized to JSON, it just looks like cleaner JSON.

    I don’t have the code in front of me, but I believe if you don’t override the ToString() the way I did you’ll just get the default object serialization rather than JSON.

Comments are closed.