Azure Cosmos DB: Build a MongoDB API console app with Golang and the Azure portal

Durgaprasad Budhwani
4 min readMay 26, 2017

--

Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. You can quickly create and query document, key/value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.

This quick-start demonstrates how to use an existing MongoDB app written in Golang and connect it to your Azure Cosmos DB database, which supports MongoDB client connections.

In other words, your Golang application only knows that it’s connecting to a database using MongoDB APIs. It is transparent to the application that the data is stored in Azure Cosmos DB.

We’ll cover:

  • Prerequisites for this tutorial
  • Creating and connecting to an Azure Cosmos DB account
  • Setting up your application
  • Connect to an Azure Cosmos DB account
  • CRUD Operations

Prerequisites for the Golang tutorial

  1. Basic knowledge of GO language
  2. Azure subscription. (If you don’t have an Azure subscription, create a free account before you begin.)
  3. IDE — Gogland by Jetbrains or Visual Studio Code by Microsoft or Atom

Creating and connecting to an Azure Cosmos DB account

  1. In a new window, sign in to the Azure portal.
  2. In the left menu, click New, click Databases, and then click Azure Cosmos DB.

3. In the New account blade, specify the desired configuration for the Azure Cosmos DB account.

With Azure Cosmos DB, you can choose one of four programming models: Gremlin (graph), MongoDB, SQL (DocumentDB), and Table (key-value).

In this quick start we’ll be programming against the MongoDB API so you’ll choose MongoDB as you fill out the form. But if you have graph data for a social media mes New app, document data from a catalog app, or key/value (table) data, realize that Azure Cosmos DB can provide a highly available, globally-distributed database service platform for all your mission-critical applications.

Fill out the New account blade using the information in the screenshot as a guide . You will choose unique values as you set up your account so your values will not match the screenshot exactly

Create Azure Cosmos DB Account

4. Click Create to create the account.

5. On the toolbar, click Notifications to monitor the deployment process.

Check Progress of Azure Cosmos DB Deployment

6. Click on golang-couch resources.

7. Get connection string information which will be required by client applications.

Connection information of MongoDB

Setting up your application

It’s time to make our hands dirty. Open your favorite editor (Gogland, VS Code or Atom). For this article, I will use Gogland editor.

  1. Create folder CosmosDBAcces folder inside GOROOT\src folder
  2. Run below command to get mgo package
go get gopkg.in/mgo.v2

Connect to an Azure Cosmos DB account

mgo (pronounced as mango) is a MongoDB driver for the Go language that implements a rich and well tested selection of features under a very simple API following standard Go idioms.

Azure Cosmos DB supports the SSL-enabled MongoDB. To connect to an SSL-enabled MongoDB, we need to define the DialServer function in mgo.DialInfo, and make use of the tls.Dial to perform the connection.

The following code Golang code snippet connects Go app with Azure MongoDB. DialInfo holds options for establishing a session with a MongoDB cluster.

Azure Cosmos DB — MGO SSL session established

mgo.Dial() method is used when there is no SSL connection and for SSL connection mgo.DialWithInfo() method is required.

Instance of DialWIthInfo{} object will be used to create session object. Once session is established, we can access collection by following code snippet

collection := session.DB(“golang-couch”).C(“package”)

CRUD Operations

1. Create Document

Create Document

2. Query/Read Document

Azure Cosmos DB supports rich queries against JSON documents stored in each collection. The following sample code shows a query that you can run against the documents in your collection.

Read Document

3. Update Document

Update Document

4. Delete Document

Azure Cosmos DB supports deleting JSON documents.

Delete Document

Get the complete Golang tutorial solution

Please have a look at the entire source code at GitHub.

--

--

Durgaprasad Budhwani

Chief Technologist @ Tech9 | Udemy Instructor | Cloud Expert | JS | React | Go | NodeJs | Youtuber | Serverless | DevOps | 2 x AWS | Azure | Google Cloud | CKAD