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,
- Introduction To Azure Cosmos DB - Day One
- Quick Start With Azure Cosmos DB - Day Two
- Quick Start With Azure Cosmos DB - Day Three
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,
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.
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.
class Transaction
{
public string id { get; set; }
public string Operator { get; set; }
public string Provider { get; set; }
public string Region { get; set; }
public string Mobile { get; set; }
public string Amount { get; set; }
public string Status { get; set; }
public string ExecutionTime { get; set; }
public DateTime CreatedDate { get; set; }
public Transaction()
{
}
}
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.
class Program
{
static string endpointUrl = string.Empty;
static string authorizationKey = string.Empty;
static DocumentClient ddbClient;
static void Main(string[] args)
{
string db = "Recharge";
string collection = "Prepaid";
endpointUrl = "https://utilitybillpayments.documents.azure.com:443/";
authorizationKey = "pPTTE4JK89I8KH5Ap1folb1rXjomWqBW0VEXPJYgqfxGLzdh5OZzgbvgecAxamxvWDi==";
try
{
Transaction txn = new Transaction();
txn.Operator = "Vodafone";
txn.Provider = "Euronet";
txn.Region = "Gujarat";
txn.Mobile = "9825134178";
txn.Amount = "200";
txn.Status = "Success";
txn.ExecutionTime = "100 ms";
txn.CreatedDate = DateTime.Now;
if (ddbClient == null)
ddbClient = new DocumentClient(new Uri(endpointUrl), authorizationKey);
ddbClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(db , collection ), txn).Wait();
}
catch (Exception ex)
{
throw ex;
}
}
}
Execute the above application and jump back to the Azure portal, there we can see that new document is created under Cosmos DB collection.
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.