Quick Start With Azure Cosmos DB - Day Four

In this article, we will create a JSON document under cosmos DB collection through the portal and will also check how we can create it through C#.

Please go through my previous articles of this series,

Create Document through Azure Portal

Log in to the Azure portal

Select Azure Cosmos DB and expand your database; i.e., Recharge

Navigate to Documents under the 'Prepaid' collection.

Click on 'New Document' which will allow writing documents in JSON format,

Azure Cosmos DB 

For each document 32 big GUID ID is assigned. You can save documents up to the size of 2 MB. Each document can be of a different schema as it is schema free. 

Azure Cosmos DB 

Create Document through Azure Portal

Create one console application in Visual Studio.

Add NuGet package Microsoft.Azure.DocumentDB

Define the model having all the fields which we want to persist under cosmos DB.

  1. class Transaction  
  2.     {  
  3.         public string id { get; set; }  
  4.         public string Operator { get; set; }  
  5.         public string Provider { get; set; }  
  6.         public string Region { get; set; }  
  7.         public string Mobile { get; set; }  
  8.         public string Amount { get; set; }  
  9.         public string Status { get; set; }  
  10.         public string ExecutionTime { get; set; }  
  11.         public DateTime CreatedDate { get; set; }  
  12.    
  13.        public Transaction()  
  14.         {  
  15.               
  16.         }  
  17.    }  

In order to create a document, first we need to connect to cosmos DB which requires the below details:

  • EndpointUrl - URL created by Azure with account name which is provided by us; i.e., utilitybillpayments
  • AuthorizationKey - Generated by Azure for the account
  • Database Name - In our case we have given 'Recharge'
  • Collection Name - In our case we have given 'Prepaid'

In the next step, we have to initialize an object of DocumentClient which expects two parameters; i.e., EndpointUrl and AuthorizationKey. Once the object is created we need to call CreateDocumentAsync method in order to create a document.

  1. class Program  
  2.     {  
  3.         static string endpointUrl = string.Empty;  
  4.         static string authorizationKey = string.Empty;  
  5.         static DocumentClient ddbClient;  
  6.         static void Main(string[] args)  
  7.         {  
  8.             string db = "Recharge";  
  9.             string collection = "Prepaid";  
  10.             endpointUrl = "https://utilitybillpayments.documents.azure.com:443/";  
  11.             authorizationKey = "pPTTE4JK89I8KH5Ap1folb1rXjomWqBW0VEXPJYgqfxGLzdh5OZzgbvgecAxamxvWDi==";  
  12.    
  13.             try  
  14.             {  
  15.                 Transaction txn = new Transaction();  
  16.     txn.Operator = "Vodafone";  
  17.                     txn.Provider = "Euronet";  
  18.                     txn.Region = "Gujarat";  
  19.                     txn.Mobile = "9825134178";  
  20.                     txn.Amount = "200";  
  21.                     txn.Status = "Success";  
  22.                     txn.ExecutionTime = "100 ms";  
  23.                     txn.CreatedDate = DateTime.Now;  
  24.    
  25.                     if (ddbClient == null)  
  26.                         ddbClient = new DocumentClient(new Uri(endpointUrl), authorizationKey);  
  27.    
  28.                       
  29. ddbClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(db , collection ), txn).Wait();  
  30.                   
  31.             }  
  32.             catch (Exception ex)  
  33.             {  
  34.                 throw ex;  
  35.             }  
  36.         }  
  37.     }  

Execute the above application and jump back to the Azure portal, there we can see that new document is created under Cosmos DB collection.

Azure Cosmos DB 

So in this way, we can persist our daily transactions in cosmos db in the form of documents. In the next article, we will see how we can execute a query on our stored transactions.


Similar Articles