Connecting to MongoLabs on heroku
For an internal Topicus application I wanted to deploy to Heroku and utilize MongoDB through MongoLabs. The main reason to use MongoLabs: they have a free 240MB database plan, and I wanted to try MongoDB to see how it would work.
Getting it to run on Heroku was kind of a challenge since I want to build a Java/Wicket application, and most documentation relies on Ruby deployments. In order to connect to MongoLabs you need to do the following:
MongoURI mongoURI = new MongoURI(System.getenv("MONGOLAB_URI")); DB connectedDB = mongoURI.connectDB(); connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
What evaded me was that you need to explicitly authenticate using the provided username and password, otherwise you will get authorization errors. The documentation didn't show this as a required step unfortunately.
Of course you should check if authentication succeeded, and that no error occurred.