Richard Bucker

Connecting to MongoLab - perl and python

Posted at — Oct 13, 2011

[update 2011.10.13] I thought I would add the following quote from the MongoLab support pages: “If you connect to your database from outside EC2 or Rackspace your data is less secure. While your database does require username / passord authentication, you are potentially vulnerable to others “sniffing” your traffic. We are currently exploring ways to provide for more secure methods of connecting to MongoLab databases from outside the cloud."I’m working on a mojolicious project as currently mentioned on this site. The next logical step for the application is a connection to the DB. I was originally going to deploy a mongoDB instance on my own server… and that would be great. But I’ve decided to use MongoLabs instead.  I suppose I could also use MongoHQ and try them independently and for comparison. That’s a story for another day.Connecting to MondoLab was pretty simple: create an account create a database create a collection create a user for interacting with the collectionThat’s it.  The nice thing is that MongoLab gives you the mongo-cli interface sample so you know exactly what’s what. From here you can test the connection on your PC if you have the mongoDB client installed.From outside of Rackspace: mongo -u <username> -p <password>From within Rackspace: mongo -u <username> -p <password>Looking at the CPAN help for MongoDB (previously installed in mojolicious part 1) So now we have to test the connection with this sample code (I got it from the CPAN and then made some corrections).use MongoDB;my $connection = MongoDB::Connection->new(host => ‘mongodb://’);$connection->authenticate(‘mydb’, ‘username’, ‘password’);my $database = $connection->mydb;my $collection = $database->get_collection(my_collection);my $id = $collection->insert({ some => ‘data’ });my $data = $collection->find_one({ _id => $id });Once I executed the program I verified that the data was written to the DB by logging into the webGUI and checking the collection. The data was there and ready.Then I took this program and made a few modifications so that I could dump the record I just inserted. The code looks like this:use MongoDB;my $connection = MongoDB::Connection->new(host => ‘mongodb://’);$connection->authenticate(‘mydb’, ‘username’, ‘password’);my $database = $connection->mydb;my $collection = $database->get_collection(my_collection);my $data = $collection->find_one();while (($key, $value) = each(%$data)){ print $key.”, “.$value."<br />";}It’s not a very sophisticated dumper and there are some good libs for that sort of thing, however, my mission was to dump the data and so I did.I’d like to take a sidebar moment to mention that I recently read an article “why perl”. The take away from the article was that perl programmers are ‘A’ and that perl programs are ‘B’. Granted there is no real evidence of this, however, there is a corollary. If you want to hire smart people who take an interest in their craft and you do not want to go through throngs of java resumes post an erlang position. So what I’m saying is that perl is a edge language where python, ruby, javascript, java are in the median space and therefore “mostly” attracting median skilled programmers.(let the trolling begin)So I implemented the same exact program (to pull the data from the DB) in python. It took half the time because I was already familiar with the syntax etc… as there is some nuance in perl that I’ve flushed from my cache I wanted to make a connection to my my python side.from pymongo import Connectionconnection = Connection(‘’,27999)db = connection.mydbdb.authenticate(‘username’,‘password’)collection = db.mycollectionprint collection.find_one()Worked like a charm. The output was prettier because python makes that easy. I’m looking forward to part 3. In the meantime I’m going to try this against mongoHQ.