How we use MongoDB in the cloud

At teamgeist.io we love the cloud and use various services and solutions to adapt and scale quickly. We develop our back end application with Java and use MongoDB as our main storage solution. So we needed a application hosting and storage solution in the cloud. With this article I am going to describe what helped us to quickly develop a great product with the support of MongoSoup - the first German-based MongoDB cloud hosting solution.

Our Cloud Solution

TL;DR

  1. Register and create your App at cloudControl or heroku.
  2. Enable the cloudControl MongoSoup add-on or heroku MongoSoup add-on
  3. Integrate the specific MongoDB driver for your programming language.
  4. Develop your application.
  5. Deploy the application to your platform.

Choose cloud hosting

When we first started developing we thought about hosting our application as well as our database ourselves. Our server with our expertise, what can go wrong? Well, possible downtime, 24/7 support etc. All the things you do not want to deal with. So we choose the cloud and decided to host our application at cloudControl which gives us basically the same functionality as heroku but with a German speaking support.

The cloudControl Quickstart documents the necessary steps for setting up an account and start with your first application.

Heroku has a Getting Started with Java tutorial which explains the first steps for a Java application on their platform.

Follow the steps for your preferred platform to install the client application and then continue with the next steps in this article.

Your App with the MongoSoup add-on

We create a new (Java) application (container) at cloudControl and add the smallest (free) MongoSoup add-on to your container with following commands:

$ cctrlapp [appname] create java
$ cctrlapp [appname]/default addon.add mongosoup.sandbox

The same thing can be done with heroku with the following commands:

$ heroku create
$ heroku addons:add mongosoup

If you need more storage space, automatic backups and premium support you can later upgrade to a bigger MongoSoup plan with ease. You might even get a dedicated solution just for you.

Integrate MongoDB driver

Depending on your build system you have to integrate MongoDB driver dependency. We are using Gradle as our build tool, however, the cloudControl and heroku buildpack are configured to use Maven. If you want to use Gradle you have to provide a custom Gradle buildpack which will work for both, cloudControl and heroku. The configuration for gradle is the following:

compile 'org.mongodb:mongo-java-driver:2.12.13'

Alternatively you could use Maven with the following configuration:

<dependency>
	<groupId>org.mongodb</groupId>
	<artifactId>mongo-java-driver</artifactId>
	<version>2.12.3</version>
</dependency>

At the time of writing this article the MongoDB Java Driver was available in version 2.12.13. Please check if there is a new version available on Maven Central.

Develop your application

Now you can develop your application that uses MongoDB as the storage engine. cloudControl and heroku, both set the environment variable MONGOSOUP_URL to get the connection URL string to access the MongoDB database provided by MongoSoup.

String uriString = System.getenv("MONGOSOUP_URL");
MongoClientURI uri = new MongoClientURI(uriString);
MongoClient mongo = new MongoClient(uri);
DB db = mongo.getDB(uri.getDatabase());

DBCollection collection = db.getCollection("testCollection");

BasicDBObject doc = new BasicDBObject("hello", "MongoSoup");
collection.insert(doc);

We at teamgeist.io are using Spring Data MongoDB to get a POJO centric model for interacting with the MongoDB collections and to write Repositories for accessing data in the database.

Deploy to your platform

Deployment with cloudControl is very straight forward and is basically just a git push. Just use the following commands to deploy and publish your application:

$ cctrlapp [appname]/default push
$ cctrlapp [appname]/default deploy

Deploying to heroku is even simpler and can be done with the following command:

$ git push heroku master

Et voilà. Your very first (cloud) application that is using MongoDB hosted in the cloud. Pretty simple and easy, though, this “Hello MongoSoup” example might be not what you will end up.

The real power will unveil as soon as scaling and availability is important. However, you see that starting your project in the cloud with MongoSoup is very simple and can be achieved with very little effort.

Share this: Facebook Twitter Google

About the author

Franziskus Domig

Franziskus Domig has been working and developing rich frontend applications for comSysto GmbH since 2012. He works with JavaScript, Java, HTML5 and Database Systems. Particularly with AngularJS, Spring, Bootstrap and MongoDB.

Related Posts