Getting Started with Azure Cosmos DB, Part 1 – CRUD

by 

| August 17, 2017 | in

Cosmos – κόσμος – the world, universe – John Jeffrey Dodson Lexicon (public domain)

Cosmos means the entire world, so I wonder if Azure Cosmos DB is Microsoft’s view of the entire world for data stores going forward. Sure, blob storage will have some particular advantages and SQL server will have some advantages too. But slowly over time, I believe we will be using Azure Cosmos DB more and more.

Why?

Well, frankly there are already some great data stores out there. There is SQL Azure for relational data. There is Azure table storage for high availability storage of documents at a low cost. But Azure Cosmos DB provides super high availability and offers a richer set of programming options. So which to choose?

  • SQL Server – Strongly typed data. Reasonable scalability requirements.
  • Cosmos DB – High scalability with rich querying options.
  • Table Storage – Cheap, but limited query options. Limited design options might cause you to duplicate your data so you can query it affectively.

In this blog post, I will cover the very basics of Azure Cosmos DB:

  • Set up an Azure Cosmos DB database
  • Write four simple Cosmos queries

Create an Azure Cosmos DB Database

Go to the Azure Portal and create an Azure Cosmos DB database.

After you create the database, you will need to create a new collection.

Now that we have a Cosmos database and a Collection, we can write some code.

Create a project in VS so we can write a simple accessor and then create a simple console application to test our code.

After creating the project, we need to add a reference to the Cosmos database nuget package.

Some Basic Azure Cosmos CRUD

Time to do some CRUD. Let’s start with Read by creating two new classes, Note and NotesAccessor. Note will be our data used by the NoteAccessor.

Defining the Note class is pretty easy, but we will decorate it with some JsonProperty attributes.

Now let’s create a method to return all notes stored in the system.

The code above is pretty basic. We first must create an instance of DocumentClient since our queries will involve it.

The endpoint and key are given to you for your Cosmos database after you create the database.

The next thing we have to create is a link to the document collection since we are querying multiple records. That is what this link is for:

Then we just need to write a query. This is easy since we are just returning all records for this method.

Then we just need to go through and read all the records (and return them).

Time for the Create.

Time for the Update.

The Delete method should look pretty familiar at this point.

This is what we see if we look at NotesAccessor as a whole:

A simple test client:

Now we can see how to do some very simple CRUD operations with Azure Cosmos DB. Stay tuned for some more advanced functions.

author avatar
Chad Michel Senior Software Architect
Chad is a lifelong Nebraskan. He grew up in rural Nebraska and now lives in Lincoln. Chad and his wife have a son and daughter.

Related posts