Using MongoHQ With CloudBees
This guide provides instructions for how to get started using MongoHQ on the CloudBees Platform for running your Java applications in the cloud. MongoHQ has a special partnership for Mongo Hosting with CloudBees that allows you to provision MongoDB databases from within the CloudBees system.
Even though you can use existing MongoHQ databases for your CloudBees application and create new MongoHQ databases from MongoHQ to use in CloudBees, these instructions will assume that you are doing everything through CloudBees.
Note: This guide assumes that you have a CloudBees account and have a basic understanding of Java and MongoDB.
Create a New CloudBees Application
To begin, let’s create a new CloudBees application from the CloudBees interface.
Log into your CloudBees account.
Click on the “Application” icon and add an application to your account.
Upon creation, the application will deploy and provide you with a URL.
Add MongoHQ to your CloudBees Account
Click the “Home” link and return back to the CloudBees main screen.
Click on the “More Services” icon.
Click on the “Add Services” button.
Find MongoHQ (look for the rocket) in the services and click on the “Learn More”.
From there, choose a plan and select “Install”.
Note: Even though you have added MongoHQ to your CloudBees account, you still have one more step: provisioning the databases.
Provisioning a MongoHQ Database
From the CloudBees main screen, click on the “Services” tab.
Under the MongoHQ listing, click on the “MongoHQ” title/link.
Click on the “create a new mongodb” link.
Select a database type, give the database a name and click the “Create” button.
Note: The name you specify in the last step is your CloudBees reference to the database. You will find that CloudBees still assigned a unique programmatic name to the database. You will need the programmatic name in your code.
Basic Example: Part A - Setting up your Project
In this basic example, we will connect to our provisioned MongoHQ database using NetBeans and a very basic system out call. This is an easy way to verify that all the settings are correct and that you are authenticating to your database properly.
Create a new project in NetBeans. Choose Java > Java Application. You can call it cloudbees_mongohq.
Add a reference to the downloaded Java driver for MongoDB. (need an image here)
Add the MongoDB driver class dependencies to your project.
To save a bit of time and keep this example simple, add these dependencies as well.
Basic Example: Part B - Connecting to your Database
Now that we have the basic dependencies in our project file, let’s set up a simple connection to the database.
In your public class, add a main method.
Add the connection information for the database.
Add an exception handler for the main method.
Basic Example: Part C - Performing Actions on the Database
Ok, so we think we are connected. Now, let’s try some basic actions on the database to verify this. Let’s connect and list out the collections. As you may know, a cleanly provisioned MongoDB database will have a system.users and a system.indexes collection.
Get the collection names.
Now, let’s do a quick iteration and display the collection names.
Now run your application and see how things work. If you see the following, congrats!
That is it. If you have any trouble, please let us know at: support@mongohq.com.