NoSQL with Azure CosmosDB

NoSQL with Azure CosmosDBMukundh BhushanBlockedUnblockFollowFollowingMar 10image from google imagesCosmosDB is the No SQL database offered by Azure.

Azure Cosmos DB is Microsoft’s globally distributed, multi-model database service.

Cosmos DB allows you to independently scale throughput and storage across the widespread Azure Data centers across the globe.

You can access data using your favorite APIs such as SQL, MongoDB, Cassandra, Tables, or Gremlin.

TLDRCodeSinging into Azure portalCreating a CosmosDB userCreating a collectionConnecting our appTesting the appLet’s get started…gif from giphyCodeThe code can be found in this repository.

Clone the repository if you would like to follow along.

I have written my application’s backend with nodejs, the following steps will work with applications written in other languages too.

Singing into Azure portalClick on this link to go to the login page.

Click on the sign in button on the top left corner of the screen.

Once you have signed in click on portal next to the sign in button.

Creating a CosmosDB userFirst, we need to find the CosmosDb service in Azure.

Click on the all service option in your portal and search for “Cosmos”Choose “Azure Cosmos DB”CosmosDB service in AzureThe page you are in now shows a list of all the users for all the databases you have and their corresponding regions.

Now we need to create the user for our database…Click on AddWe now are given with a form to fillFill in the following details:Subscription: Lets azure know on which of the subscription to bill this web app on.

In my case, I choose “Pay-As-You-Go Dev/Test”Resource group: in a nutshell, they help in making a logical group of the services that you are using for an application they can contain web apps, databases or other Azure services.

I chose “Create new”.

If you have selected “Create new” you would have to name your resource group.

I name mine “monogomed”.

Account name: this is the username through which we can access the database.

I named mine “cosmosuser”API: choose “Azure Cosmos DB for MongoDB API”Location: Choose a location which suits your application best or a location where you are near toGeo-Redundancy: disableMuli-regionWrites: disableNote: As the application, I am building is only for experimental purposes that’s why I am disabling these features.

Do enable them if your applications require these featuresYour form should look something like this…Form after fillingClick on “Review+create” once you have filled all the details.

It should not take more than 5 minutes to deploy.

Now we need to create a collection…Once the user is created go ahead and click on itNow we need to create a collection to store our data inUnder the overview tab choose add collectionAdding a collectionNow you are in the “Data Explorer” it lists all the collections and their corresponding records.

By default, a collection called “admin” is createdClick on “New Collection”You are now to fill the form with the information regarding the collection.

Fill in the following details:Database_Id: the name of the database this collection is under.

Create a new one if you don’t have any.

I named mine “cosmodb_medium”Collection_Id: name of the collection to be created.

I named mine “users”Storage capacity: choose the one which best suits your application.

I chose “Fixed” as it costs less.

Throughput: is the measure of request units/ sec.

Choose this value based on your application load.

I chose the least which is 400.

Unique Keys: This allows you to add additional security layers before accessing your this collection specifically.

I would not be adding any for my sample application.

Click “OK” once you have finishedTo check if your collection has been added.

Go back to the “Data Explorer” tab our new collection must be present.

Connecting our appIn the settings panel under Connection Strings, the primary and secondary keys can be found along with the port, hostname and other information which will be needed to get connectedIf you want to keep all the keys separated from the form the actual source files then this is the format of the connectionmongodb://<account name>: <primary/secondary key> @ <accountname>.



com : <port number>/<database name>?.ssl=<true|false>Note: special charecters in the keys may throw an error if written in plain text form.

They need to be in the URL encoded form.

eg “=” is “%3D”If you are planning to have the connection string in your application file then copy the connection stringNote: Once you have pasted the connection string the database name must be added to the string.

Before the “?ssl” field type in the database name.

Testing the appStart the application that you have written.

On navigating to the “Data Explorer” tab choose the corresponding database and the collection.

Our records will be saved under the file named “Documents”We have not added any record to the database so it must be empty for nowempty collectionOn sending a post request with the form data.

I am using postman to send data instead of an HTML form nevertheless it would work either waypost request with form dataThe collection has been updated with the latest valuesupdated collectionI updated the table with one more record.

Now let's try getting all the records from the collection.

A GET request will display all the records in the collection.

Getting data from the collectionCongratulations YOU DID IT!!!Thanks for sticking till the end….. More details

Leave a Reply