How to connect to MongoDB Atlas with Ballerina

Manuri Amaya Perera
Ballerina Swan Lake Tech Blog
2 min readNov 2, 2018

Ballerina is a programming language designed to fill the gap between integration focused products and general purpose programming languages. Read more here. MongoDB Atlas is a fully managed cloud database service developed by the MongoDB folks. Lets see how to connect to MongoDB Atlas cluster and perform some operations, using MongoDB package provided by WSO2.

  1. First install Ballerina! I used Ballerina 0.982.0 version. You can download the installer from here.

2. Then download MongoDB package from here, and install it using the installation script.

Now I assume you have a MongoDB Atlas account. Its quick and easy to create one. Visit their website and create a free account if you don’t have one.

Then, make sure to add your IP into the whitelist in your MongoDB atlas cloud configuration. There is an option which detects your public IP and adds it, as well.

Also you’ll have to create a user as well. The user interface is quite intuitive. I don’t think you need instructions for that. :)

Now you should be able to view your clusters as below.

Now we want to get the connection string.

To do that,

  1. Click on the connect button.
  2. Then you will see the following UI. Click on Connect Your Application option.

3. Then you’ll see the UI below. There will be two options for the connection string. You can use either of them.

Now you can try out the following Ballerina program which demonstrates creating the endpoint and performing all available operations provided by the WSO2 MongoDB package.

Note: WSO2 MongoDB package allows you to either connect with direct URL string or by filling in required properties (host, port,.. etc) separately. The example demonstrates both.

This program inserts 3 documents to the collection “Books”, queries for them, and deletes one of them. So after running the program, you should be able to see two documents being inserted in your MongoDB Atlas cluster as follows.

That’s about it. Enjoy!

--

--

Manuri Amaya Perera
Ballerina Swan Lake Tech Blog

I am an Engineer at WSO2. Currently working in the Ballerina team. Mainly contributing to Ballerina data client area. My GitHub URL: https://github.com/manuri