VoiceGap

Filling the space between You and Your Apps

How to setup a remote database in MongoDB

In this article we will examine how to quickly set up a remote database mechanism built upon our model of GET, PUT, and TAKE. We will use http://www.mongodb.org/ to accomplish this task.   In our previous example we were using MySql as a backing store.   Making extensive use of the INSERT – ON DUPLICATE pattern.

This time we will make use of the UPSERT pattern; which is basically the same thing.   If you find the key UPDATE it, if not, INSERT it.   It sure sounds the same.   While MongoDB may be NOSQL, it it doesn’t stray too far from traditional data stores (INSERT, SELECT/FIND, UPDATE, DELETE/REMOVE).

The first step is to find a place to run the MongoDB.  I have chosen Red Hat’s OpenShift.   A complete step by step tutorial is available here -> https://openshift.redhat.com/community/blogs/deploying-a-php-twitter-app-in-the-cloud-with-mongodb-openshift, but I will provide a brief synopsis of the steps.

You have to create an OpenShift account, and you have to install the development tools on your development machine.   As VoiceGap deals with Android development, a Cygwin installation was readily available.  However, installing the tools was not a completely simple task; but there are lots of blogs to help you out!

The next step was creating the crypto keys.   I can save you lots to time and warn you now that the keys are stored in two DIFFERENT places by two different tools.  So always know, and USE, the precise key file name and directory whenever possible and not go with the default.

We are going to install the sample PHP application that is described in the link above called PHPMongoTweet. This application allows you to search for things on Twitter and have the resulting tweets automatically added to your MongoDB collection. As I ran into a few issues with ‘permission denied’ I’ve included my steps to recreating an rsa key.

rhc sshkey add -i chuck
exec `ssh-agent`
ssh-add.exe ~/.ssh/chuck

rhc-create-app -a ptweet -t php-5.3

rhc-ctl-app -a ptweet -e add-mongodb-2.0

cd ptweet
git remote add upstream -m master git://github.com/openshift/phpmongotweet-example.git
git pull -s recursive -X theirs upstream master
git push

These commands will pull a copy of the phpmongotweet app from from Github and place it in your applications git repository. The ‘git push’ command deploys your new application. You should now be able to visit http://ptweet-emoxsha.rhcloud.com. The output from the git push should include a link that you can copy and paste. Once the page loads you can search for anything on twitter and the tweets will be loaded into the MongoDB database associated with your search string.

Wow!     If not for the RSA Keys, that was way too easy.   A complete web application and MongoDB installation in less time than it took to finish your lunch!    At this point you have a complete working copy of the PHPMongoTweet Example on your computer.   The next step is to add out PHP code from earlier.

We need to swapout the credentials, and the database calls to conform to MongoDB.   Keep in mind that we created our own KEY model.   However, it is separate from the MongoDB ‘_id’ as we use three separate concepts (Relation, AppId, and then unique Key). We expect the keys to be unique by definition as duplicates are written over.  Let’s look at the PUT function first.

<?php
include 'common.php';
$rel = $_GET[rel];
$appid = $_GET[appid];
$key = $_GET[key];
$data = $_GET[data];
$nosql = array("rel"=>$rel,"appid"=>$appid,"key"=>$key);
$nodata =  array('$set' => array("user-data" => $data));
?>
<?php
$collection = get_collection(TWEETS_COLLECTION);
$collection->update($nosql, $nodata,array("upsert" => true));
echo json_encode($collection->findOne($nosql));
?>

Then the GET function.  We should be able to exactly mimic the functionality of our original design passing back a JSON formatted object.

<?php
include 'common.php';
$rel = $_GET[rel];
$appid = $_GET[appid];
$key = $_GET[key];
$nosql = array("rel"=>$rel,"appid"=>$appid,"key"=>$key);
?>
<?php
$collection = get_collection(TWEETS_COLLECTION);
echo json_encode($collection->findOne($nosql));
?>

The TAKE function is a bit new.  It was added during the tail end of the Rosie project.   The basic premise is that IF a KEY exists, send it back to me and clear it out so that no one including myself will be able to GET it.     This technique is used in Artificial Intelligence and distributed computing as both a Rendevous mechanism and a Task Distribution mechanism (more on these topics in the future).

<?php
include 'common.php';
$rel = $_GET[rel];
$appid = $_GET[appid];
$key = $_GET[key];
$nosql = array("rel"=>$rel,"appid"=>$appid,"key"=>$key);
?>
<?php
$collection = get_collection(TWEETS_COLLECTION);
$item=$collection->findOne($nosql);
echo json_encode($item);
$collection->remove($item);
?>

There is even less code than before. If everything works as planned.  We should only have to change the URL of the web service and everything should work the way it did before.

function remote_save(u)
{
$.getJSON("http://ptweet-emoxsha.rhcloud.com/put.php?rel=mylist&appid=dev001|a1&key=test"+22+"&data="+u,{},{});
}

In summary,  we are continuing our use of both NOSQL concepts and Remote Object Stores in a Tuple-like manner.   Multiple mobile devices, web pages, and even robots can now share this information to coordinate activities.   Stay tuned for the Voice Enabled video of Rosie Roboto controlling the real robot.

Comments are currently closed.