Azure Cosmos DB - Part Three - Getting Started With Azure Document DB

Understanding the Azure Document DB

Azure Document DB is a Document based No SQL subset of CosmosDb, so if you’re not familiar with it, I request you to go through my following articles,

Azure DB is a subset of Azure DocumentDB, and a schema-free database that combines rich and familiar SQL query capabilities with consistent low latencies on JSON-based data models, targeting solutions for web, mobile, gaming, and IoT. Since DocumentDB is schema agnostic, this makes it extremely easy to adjust and adapt data models on the fly as business logic and application scenarios change.

With DocumentDB, you no longer have to deploy constant updates to the data-tier and worry about managing schema and relationships. All content in DocumentDB is automatically indexed, which makes it possible to query data across entire structures at any time. The term “NoSQL” is more of a marketing buzzword, and actually means “no requirement for entity relationships and secondary indexing” in order to query information.

Since DocumentDB is a JSON-based storage mechanism, accessing and interacting with data can be accomplished through most standards-based interfaces, as well as robust toolsets and SDKs. Although native Universal Windows Platform support is not currently available, support for virtually any other development scenario is available. Deeper level features of are currently exposed via JavaScript, such as stored procedures, triggers and user defined functions (UDFs) making it easy for developers to write application logic that can be packaged and executed directly on database storage partitions.
Azure
Terminologies related to DocumentDb
Azure
Database

A database is a logical container of document storage partitioned across collections. It is also a user's container.

User

The logical namespace for scoping permissions.

Permission

An authorization token associated with a user for access to a specific resource.

Collection

A collection is a container of JSON documents and the associated JavaScript application logic.

Stored Procedure

Application logic written in JavaScript which is registered with a collection and transactionally executed within the database engine.

Trigger

Application logic written in JavaScript executed before or after either an insert, replace or delete operation.

UDF

Application logic written in JavaScript. UDFs enable you to model a custom query operator and thereby extend the core DocumentDB query language.

Document

Arbitrary user-defined JSON content. By default, no schema needs to be defined nor do secondary indices need to be provided for all the documents added to a collection.

Hands On Demo

We are trying to create a MVC application and showcase the simple CRUD operations with Cosmos DB.

  • Go to Azure Portal and search for cosmos db.

    Azure

  • You will get four options: Gremlin, MongoDb, SQL (DocumentDB) and Table (keyValue Pair). As we are working with DocumentDb so we are going to select the documentdb option and supply the necessary information like the resource group and where do you want to host your db.

    Azure

  • Once the account is created, go to the quick start option in the properties menu and then click on Create Items Collection Button. This will create a Collection in the database and then download the sample application.

    Please note that you can either use the sample application or create your own application and add the documentDb client SDK via nugget. For this demo, we are going to continue with the sample application.

    Azure

  • Open your project in Visual Studio and go to web.config. You can see that the endpoint, the authKey, Database and Collections are prefilled with the information from your cosmosdb. If you’re developing your application from scratch then you need to pass on this information to your web.config.

    Azure

  • The Item Collection that we have created has a corresponding controller in our MVC application sample and it contains most of the common CRUD scenarios covered and its calling the documentdb repository in its actions Items.
    1. public class ItemController : Controller  
    2.     {  
    3.         [ActionName("Index")]  
    4.         public async Task<ActionResult> IndexAsync()  
    5.         {  
    6.             var items = await DocumentDBRepository<Item>.GetItemsAsync(d => !d.Completed);  
    7.             return View(items);  
    8.         }  
    9.  
    10. #pragma warning disable 1998  
    11.         [ActionName("Create")]  
    12.         public async Task<ActionResult> CreateAsync()  
    13.         {  
    14.             return View();  
    15.         }  
    16. #pragma warning restore 1998  
    17.   
    18.         [HttpPost]  
    19.         [ActionName("Create")]  
    20.         [ValidateAntiForgeryToken]  
    21.         public async Task<ActionResult> CreateAsync([Bind(Include = "Id,Name,Description,Completed")] Item item)  
    22.         {  
    23.             if (ModelState.IsValid)  
    24.             {  
    25.                 await DocumentDBRepository<Item>.CreateItemAsync(item);  
    26.                 return RedirectToAction("Index");  
    27.             }  
    28.   
    29.             return View(item);  
    30.         }  
    31.   
    32.         [HttpPost]  
    33.         [ActionName("Edit")]  
    34.         [ValidateAntiForgeryToken]  
    35.         public async Task<ActionResult> EditAsync([Bind(Include = "Id,Name,Description,Completed")] Item item)  
    36.         {  
    37.             if (ModelState.IsValid)  
    38.             {  
    39.                 await DocumentDBRepository<Item>.UpdateItemAsync(item.Id, item);  
    40.                 return RedirectToAction("Index");  
    41.             }  
    42.   
    43.             return View(item);  
    44.         }  
    45.   
    46.         [ActionName("Edit")]  
    47.         public async Task<ActionResult> EditAsync(string id)  
    48.         {  
    49.             if (id == null)  
    50.             {  
    51.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
    52.             }  
    53.   
    54.             Item item = await DocumentDBRepository<Item>.GetItemAsync(id);  
    55.             if (item == null)  
    56.             {  
    57.                 return HttpNotFound();  
    58.             }  
    59.   
    60.             return View(item);  
    61.         }  
    62.   
    63.         [ActionName("Delete")]  
    64.         public async Task<ActionResult> DeleteAsync(string id)  
    65.         {  
    66.             if (id == null)  
    67.             {  
    68.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
    69.             }  
    70.   
    71.             Item item = await DocumentDBRepository<Item>.GetItemAsync(id);  
    72.             if (item == null)  
    73.             {  
    74.                 return HttpNotFound();  
    75.             }  
    76.   
    77.             return View(item);  
    78.         }  
    79.   
    80.         [HttpPost]  
    81.         [ActionName("Delete")]  
    82.         [ValidateAntiForgeryToken]  
    83.         public async Task<ActionResult> DeleteConfirmedAsync([Bind(Include = "Id")] string id)  
    84.         {  
    85.             await DocumentDBRepository<Item>.DeleteItemAsync(id);  
    86.             return RedirectToAction("Index");  
    87.         }  
    88.   
    89.         [ActionName("Details")]  
    90.         public async Task<ActionResult> DetailsAsync(string id)  
    91.         {  
    92.             Item item = await DocumentDBRepository<Item>.GetItemAsync(id);  
    93.             return View(item);  
    94.         }  
    95.     }  

  • Going to the DocumentDbRepository which acts as an interface between our application and the documentdb. It has all the definitions of CRUD operations in the database and it also takes care of creating a new db/collection if they don’t exist, by doing a sanity check.
    1. private static readonly string DatabaseId = ConfigurationManager.AppSettings["database"];  
    2.         private static readonly string CollectionId = ConfigurationManager.AppSettings["collection"];  
    3.         private static DocumentClient client;  
    4.   
    5.         public static async Task<T> GetItemAsync(string id)  
    6.         {  
    7.             try  
    8.             {  
    9.                 Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));  
    10.                 return (T)(dynamic)document;  
    11.             }  
    12.             catch (DocumentClientException e)  
    13.             {  
    14.                 if (e.StatusCode == System.Net.HttpStatusCode.NotFound)  
    15.                 {  
    16.                     return null;  
    17.                 }  
    18.                 else  
    19.                 {  
    20.                     throw;  
    21.                 }  
    22.             }  
    23.         }  
    24.   
    25.         public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate)  
    26.         {  
    27.             IDocumentQuery<T> query = client.CreateDocumentQuery<T>(  
    28.                 UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),  
    29.                 new FeedOptions { MaxItemCount = -1 })  
    30.                 .Where(predicate)  
    31.                 .AsDocumentQuery();  
    32.   
    33.             List<T> results = new List<T>();  
    34.             while (query.HasMoreResults)  
    35.             {  
    36.                 results.AddRange(await query.ExecuteNextAsync<T>());  
    37.             }  
    38.   
    39.             return results;  
    40.         }  
    41.   
    42.         public static async Task<Document> CreateItemAsync(T item)  
    43.         {  
    44.             return await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), item);  
    45.         }  
    46.   
    47.         public static async Task<Document> UpdateItemAsync(string id, T item)  
    48.         {  
    49.             return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), item);  
    50.         }  
    51.   
    52.         public static async Task DeleteItemAsync(string id)  
    53.         {  
    54.             await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));  
    55.         }  
    56.   
    57.         public static void Initialize()  
    58.         {  
    59.             client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"]);  
    60.             CreateDatabaseIfNotExistsAsync().Wait();  
    61.             CreateCollectionIfNotExistsAsync().Wait();  
    62.         }  
    63.   
    64.         private static async Task CreateDatabaseIfNotExistsAsync()  
    65.         {  
    66.             try  
    67.             {  
    68.                 await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId));  
    69.             }  
    70.             catch (DocumentClientException e)  
    71.             {  
    72.                 if (e.StatusCode == System.Net.HttpStatusCode.NotFound)  
    73.                 {  
    74.                     await client.CreateDatabaseAsync(new Database { Id = DatabaseId });  
    75.                 }  
    76.                 else  
    77.                 {  
    78.                     throw;  
    79.                 }  
    80.             }  
    81.         }  
    82.   
    83.         private static async Task CreateCollectionIfNotExistsAsync()  
    84.         {  
    85.             try  
    86.             {  
    87.                 await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));  
    88.             }  
    89.             catch (DocumentClientException e)  
    90.             {  
    91.                 if (e.StatusCode == System.Net.HttpStatusCode.NotFound)  
    92.                 {  
    93.                     await client.CreateDocumentCollectionAsync(  
    94.                         UriFactory.CreateDatabaseUri(DatabaseId),  
    95.                         new DocumentCollection { Id = CollectionId },  
    96.                         new RequestOptions { OfferThroughput = 1000 });  
    97.                 }  
    98.                 else  
    99.                 {  
    100.                     throw;  
    101.                 }  
    102.             }  
    103.         }  
    104.     }  
  • I want to make you understand the few line of code here which is not plain C# but has to do with the understanding of the documentDb.
    1. UriFactory.CreateDatabaseUri(DatabaseId),  
    2. new DocumentCollection { Id = CollectionId },  
    3. new RequestOptions { OfferThroughput = 1000 });  
    The above lines are creating a database and creating a collection inside it but here, it's also setting the throughput of the Database, which means how many requests a database can handle concurrently. Here we are mentioning the throughput as 1000.

    P.S - The billing cost of the database is directly proportional to the throughput of the database so please make sure that you’re handling it properly and the minimum throughput is 400 that’s set by Microsoft for the documentDb Database.

  • Click on the Create New Button which will give you a UI to enter the information such as Name, Description and IsCompleted. Click on the save button and your record will be added to the database.

    Azure

  • You will be redirected to the details view and you can see that the data that we saved is reflected on the UI.

    Azure

  • Let’s go to the Data Explorer on the Azure portal and if you’re an existing customer of Document Db you might realize that by moving it under the CosmosDb hood, they have improved the data explorer.

    Azure

  • You can see that the document now exists in our database and the transaction was successful. If you look closely at the stored procedures, UDF and Triggers are all present at collection level and we are going to talk about them in another post.

Summary

Azure DocumentDB is a fully managed NoSQL document-based “database as a service” built for ultra-fast and predictable performance, high availability, elastic scaling, and global distribution, and is especially focused on ease of development.

The heireachy of the Azure DocumentDB is as follows - Account-> Database -> Collection -> Document -> Attachments.

We have gone through the demo where we tried to do the basic CRUD operations in DocumentDB.

Up Next
    Ebook Download
    View all
    Learn
    View all