August 26, 2021

Overview on adding custom columns in Azure Cosmos DB

Introduction

We recently implemented a console application for a Postal and Parcel company based out of Melbourne, Victoria, Australia. In this, we have to store the user's login track details to a Cosmos DB. So we needed to add few custom columns in the Azure Cosmos DB container to store this type of information.


In this blog, we will look into the overall structure of the Cosmos DB container and we will be creating few custom columns also.

Solution

  • The Cosmos DB is a schema-free and unstructured database. It means that we can store data without considering its format.
  • The Cosmos DB has tables as containers for the database.
  • The container stores data in JSON format. So, we can store JSON objects as an item in the container of Cosmos DB. 
  • We can add any number of custom columns and their values as a JSON property.
  • The Cosmos DB container requires only one mandatory property it's the Item ID ("id"). and the Item ID is a not null and unique field. It has 255 characters limit.

Creating an item with custom columns

First, we need to pass a JSON format object to create a new item as shown below. 

 {    

   "id": "1",    

   "User name": "Aniket Chauhan",    

   "Email Address": "aniket.chauhan@binaryrepublik.com",    

   "Logged Date Time": "2021-07-09T09:07:19.2825515Z",    

}   

Now when we save this new item it will be stored with some system-generated fields as shown below. 


Here, the User name, Email Address, and Logged Date Time properties are custom columns.
When we create an item in the cosmos DB container it generates 5 extra properties.
  1. _rid: The resource ID (_rid) is a unique identifier that is used internally for navigation and placement of the resource.
  2. _self: It is the unique addressable URI for the item.
  3. _etag: Entity tag(_etag) used for optimistic concurrency control.
  4. _attachments: It specifies the addressable path for the attachment resource.
  5. _ts: It stores the timestamp of the last update of the item.
Note: We can also pass the GUID for the "id" field as per our requirement.

Conclusion

This is how we can add custom columns in Cosmos DB. Hope this helps, good day!


If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment