Assuming that you have already created your Cosmos DB account or have installed the local Cosmos DB emulator, let us take the first step in our journey of working with Cosmos DB.
The first step is creating some data and for that, we need to have something that contains this data. This is how the concept of collections comes into picture.
To create a collection, we get the option to create a New Collection on the data explorer tab which looks like this:
Step one is to provide the id for the database that we need to create and then provide the collection id. In case, we do not have multiple collections, then the data and collection are almost the same things. However, we can definitely have multiple collections inside the database.
Cosmos DB used to provide the feature to choose the storage capacity from two
– Fixed (10 GB) i.e. one physical partition
But now, this feature has been deprecated as it scales down the elasticity. The minimum throughput has now been lowered to 400 RUs(Request Units). See the next image for reference.
Also, it is always better to avoid creating different fixed size collections when cosmos Db can do the whole work if we choose Unlimited storage capacity and all the partitions for multiple collections will be created.
we let Cosmos DB do the partitioning for us, we do need to specify the
partition key on the basis of which it will classify the data into different
collections. It is used to group multiple partitions together.
So the format to do that is as shown in the image above, to use /address/zipcode. So all the data that will belong to a particular zip code will be allocated to one collection and as the data will grow later, Cosmos DB will assign it automatically by simply looking at the Partition key of that.
A simple example of this is here:
Next comes throughput, where in we request for the performance level that we need for the project. This performance level is measure in the form of RUs (Request Units). On the local emulator , any throughput value wouldn’t matter but on a real Cosmos DB account, we need to pay for the performance level we request for. That means that if the throughput value is low, we pay less cost for low performance. For greater value of throughput, there is high performance and therefore, high cost for the performance. For the same reason, it is preferred to create multiple collections inside the same database.
You can see we have provided maximum throughout, reason being, on the emulator, whatever no. of RUs you provide, it really doesn’t matter, so we can have it maximum.
After we have created the collection, it looks to be like:
We can see the database and the collection being created. When we expand the collection, there we get the option to create documents.
Creating the documents is an important furtherance to our work with Cosmos DB which we will discuss in the next blog post.