Following on from my previous post where I explored Heroku + Node.js + Pusher I thought I would explore MongoDB.
Heroku currently supports two add-ons for MongoDB, there is MongoHQ and MongoLab. I choose MongoLab for no other reason than first impressions (and it worked first time).
There are bunch of good blog posts out there that provide examples of Heroku + Node.js + MongoDB, which helped me get started. What I discovered was a bunch of different Node.js libraries for working with MongoDB. Specifically:
So what I thought I would do here is write a series of blog posts that take the same example and show the different styles of the above libraries so you can compare and contrast. In this blog post I’ll start with the node-mongodb-native driver.
I’ll start with a disclaimer, this is not intended to demonstrate well crafted code, it is a spike, which is intended to prove the technology works. Use it as a reference example, but don’t use it as production code.
Deploying the app
git@github.com:p15martin/BlogMongoDbNativeDriver.git
- If you don’t already have an account on Heroku then create one and install the command line client (CLI)
- Move to your command prompt and change directory to where you cloned the code
- Create a new app on Heroku:
heroku create --stack cedar
- Take a note of the app that was created (e.g. hollow-autumn-6787) and the remote repository (e.g. git@heroku.com:hollow-autumn-6787.git)
- Add your Heroku app as a remote repository (remember to change it to your remote repository):
git remote add heroku git@heroku.com:hollow-autumn-6787.git
heroku addons:add mongolab:starter
- Push your code to Heroku:
git push heroku master
- Now test your app (assuming you have curl installed), remember to change the url to your Heroku app:
curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "add", "params": ["Peter", "Martin"], "id":2 }' http://hollow-autumn-6787.herokuapp.com
- If it worked then you should receive this response (if not jump down to the troubleshooting section): {“result”:”success”,”id”:2,”jsonrpc”:”2.0″}
Verify it worked
- In your browser, log on to Heroku and select your app (e.g. hollow-autumn-6787)
- From the add-ons menu select MongoLab

- You should see a screen similar to the following, under collections, there should be a collection called contacts and to its right the number of documents

- If you run curl again and refresh the page then the number of documents should be incremented
- You can also click the collection to view the documents
Comments on the code
I didn’t really like the examples I saw for the node-mongodb-native driver due to the deep callback structures. It hinders readability of the code. The documentation was also poor, although the tests are a good source of documentation.
I did some refactoring as I wanted to see how I could improve readability, as a result you will find two versions. There is web.js that uses top-level functions, and deep.js that uses closures. They both work so you can alternative between them by updating the Procfile.
On my list of things to do is to think more about writing well crafted code for Node.js. What are your experiences with writing clean and efficient code?
Troubleshooting
If you run in to trouble then check the logs. You can configure the logging level from the command line as follows:
heroku config:add LOG_LEVEL=DEBUG
To check the logs use:
heroku logs