Getting Started With Microsoft's Azure DocumentDB in the Cloud

Introduction

This article explains how to get started with Microsoft Azure DocumentDB and some changes in coding to use this new Azure service with C#, DocumentDB SQL and LINQ. DocumentDB supports SQL syntax to query JSON documents and so enables you to leverage existing SQL knowledge when working with a NoSQL database.

DocumentDb is available only as an Azure service. It is a highly manageable and schema-free JSON document database that uses a JavaScript and HTTP REST API.

It allows us to use server-side JavaScript to add user-defined functions in our SQL queries.

We can also create Stored Procedures and triggers in JavaScript. It allows us to work with the following languages and platforms:

  • .NET, including support for LINQ
  • Client-side JavaScript
  • Node.js
  • Python

Let's get started by creating a new Azure account (free one month trial) In the Azure Preview portal.

microsoft Azure

Click on New and select Document DB as shown below, enter a preferred name and the desired configuration based on your requirements. Here I have given the default 1 configuration since it is enough for sample code.

new documentDB

After providing the required inputs Azure finishes creating the DocumentDb. Now we can click on the tile and get our URI and Primary key values.

creating documentDB

Here I have used https://sample.com:443/ for the URI and DocumentDBExample for the Primary key. You will need to configure your own values.

Now let's open Visual Studio 2013 and create a new Visual C# Windows Console application project. Then, run the following command within the Package Manager Console.

install microsoft azure

We can check the latest version and the version history for this NuGet package online.

Now, add a reference to System.Configuration. Here I used the URI to the store, also known as the endpoint URL and the authorization key to access the DocumentDB service.

Add an Application Configuration File item named appSetting.config to the project, specify the URI value in the EndPointUrl key and the primary key value in the AuthorizationKey key.

The following lines show the contents for the appSetting.config file with "https://sample.com:443/" as the EndPointUrl value and "DocumentDBExample"as the AuthorizationKey value. Don't forget to replace these values with the values we copied from the Azure management portal. The DatabaseId and CollectionId keys define the ids for the database and the collection that the code will create and use.

  1. <appSettings>    
  2.   <!-- Replace the value with the value we copied from the Azure management portal -->    
  3.   <add key="EndPointUrl" value="https://sample.com:443/"/>    
  4.   <!-- Replace the value with the value you copied from the Azure management portal -->    
  5.   <add key="AuthorizationKey" value="DocumentDBExample"/>    
  6.   <add key="DatabaseId" value="Entertainment"/>    
  7.   <add key="CollectionId" value="XXXX"/>    
  8. </appSettings>  
Now open the App.config file and add the following line in the configuration section. This way, we can specify that the appSettings file name is appSettings.config.
  1. <appSettings file="appSettings.config" />   
We'll need the following references in our C# application:
  1. using Microsoft.Azure.Documents;   
  2. using Microsoft.Azure.Documents.Client;   
  3. using Microsoft.Azure.Documents.Linq;   
Summary

In this article we learned how to work with an Azure DocumentDB.

Up Next
    Ebook Download
    View all
    Learn
    View all