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.
- _rid: The resource ID (_rid) is a unique identifier that is used internally for navigation and placement of the resource.
- _self: It is the unique addressable URI for the item.
- _etag: Entity tag(_etag) used for optimistic concurrency control.
- _attachments: It specifies the addressable path for the attachment resource.
- _ts: It stores the timestamp of the last update of the item.
Conclusion
This is how we can add custom columns in Cosmos DB. Hope this helps, good day!