At this year’s Build developer conference, Microsoft announced a new addition to its Azure database portfolio. It introduced Azure Cosmos DB aimed at large and globally distributed data intensive applications and addressing a lot of developer pain points.
Microsoft’s new database offering provides a 99% uptime and minimal latency SLA. It’s even migrating all the existing document DB (NoSQL database) customers to the new Cosmos DB, free of charge. The new DB is capable of handling any data type including key-value, columnar and graph type. Developers can scale horizontally with ease and associate different geographical regions with the DB account using a single API call.
Azure Cosmos DB
Want to build a web application using .NET for your business, consult our .NET experts now!
Microsoft Azure Cosmos DB further supports popular database APIs like MongoDB, Gremlin and Azure tables. Take a look at the tutorial below to learn how you can create a Cosmos DB account within the Azure portal and use the MongoDB API to deploy a task-based application:
Setting | Suggested Value | Description |
ID | Unique Value | Enter a unique name for creating an Azure Cosmos DB account. documents.azure.com will be appended to your ID to create a URL. So, choose a unique but identifiable ID. |
API | MongoDB | The programming will be done against MongoDB API later in this tutorial. |
Subscription | Your Subscription | The Azure subscription that you want to choose for the Azure Cosmos DB account. |
Resource Group | Same value you have entered in ID | A new resource group name for your Azure Cosmos DB account. To keep things simple, you may choose the same name as your ID. |
Location | The region closest to your users | The geographical location where your Azure Cosmos DB account will be hosted. Choose the location that is in proximity to your users. This will provide them the fastest access to your data. |
Now clone a MongoDB API app from GitHub, set the connection string, run it, and follow the below steps.
git clone https://github.com/Azure-Samples/azure-cosmos-db-mongodb-dotnet-getting-started.git
A quick review of the code is essential to identify what’s happening in the application. Open the Dal.cs file under DAL directory.
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress(host, 10255);
settings.UseSsl = true;
settings.SslSettings = new SslSettings();
settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;
MongoIdentity identity = new MongoInternalIdentity(dbName, userName);
MongoIdentityEvidence evidence = new PasswordEvidence(password);
settings.Credentials = new List()
{
new MongoCredential(“SCRAM-SHA-1”, identity, evidence)
};
MongoClient client = new MongoClient(settings);
private string dbName = “Tasks”;
private string collectionName = “TasksList”;
var database = client.GetDatabase(dbName);
var todoTaskCollection = database.GetCollection(collectionName);
collection.Find(new BsonDocument()).ToList();
Now return to the Azure portal to get information on connection string and copy it into the app.
Finally, you have updated your app with all the information it requires to communicate with the Azure Cosmos DB.
Hopefully, this tutorial might have helped you to create an Azure Cosmos DB account of your own and run a web app using the MongoDB API. After running the app, you can import the additional data to your Azure Cosmos DB account and review the SLAs (Service Level Agreements) to ensure business continuity and high application performance. If you’re encountering hassles while creating an account or building an app using Azure Cosmos DB, consult a professional .NET development company like Rishabh Software or drop us a message in chat today.
Talk to our experts to get the best Microsoft Azure solution for your enterprise.