Introduction to Cloud9 and MongoDB
23 Aug 2013
A while ago MongoHQ wrote a great article as an introduction to use MongoDB as a service on Cloud9. In the meanwhile we’ve improved our basic MongoDB on Cloud9 IDE support, but we wanted to repost their solution below to show again how easy it is to use MongoDB all in the cloud!
Before we begin, you’ll need a MongoHQ account. In this example we’ll use node.js for all code, and we’re assuming you’ve already created your (node.js) workspace.
First thing to do in the Cloud9 Terminal is install MongoSkin. Just type in the Terminal:
If you want to use something else, like Mongoose, or the raw MongoDB driver, that’s OK, too. I like MongoSkin because it straddles that fine line between driver power, without being an overbearing object mapper.
Setup MongoDB connection
We now need to get the connection URL from your MongoHQ database. Log into MongoHQ, select your database, and click the Database Info tab. Put in your username and password into the URL. Use this url to build your connection object in your Cloud9 project.
Now we just need to get our collection, and perform an action on it, like find or insert.
Then it’s a simple matter of attaching a callback to whatever form we want the data.
Just to make this a little extra delicious, let’s wrap our database call in an http server, and output our response to the client.
And that’s all there is to get a MongoDB running with Cloud9 and MongoHQ!