MongoDB 2.2 Aggregation Framework Now Enabled on OpenShift PaaS - Archived

MongoDB is an integral part of the internals and over all architecture of the OpenShift platform as a service (PaaS). We have learned first hand about the power of using this open source NoSQL database and how it can help speed up development time. Today we are happy to announce that OpenShift supports the MongoDB 2.2 database engine for our users!

What does this mean for you?

If you are an existing user, sit back and relax as we have you covered. We have upgraded all existing applications using MongoDB 2.0 to the most recent 2.2 GA release. You can begin taking advantage of the latest features right away. You don’t have to worry about upgrading or migrating your data. This is one great example of the benefits of running your application code in a managed platform as a service environment.

If you are a new user, let’s get started with using MongoDB 2.2 on OpenShift.

Before we walk through the steps of adding MongoDB 2.2 to your application, let’s go over some of the new features that are part of the latest MongoDB cartridge.

New aggregation framework

The most sought after feature that we have heard about from our MongoDB users is the ability to take advantage of the aggregation framework that 10gen has been working on to make reporting-type queries easier for the users of the database.

The aggregation framework provides a mean to calculate aggregated values without having to use the map-reduce feature. The map-reduce feature has often been viewed as on overly complex system when the user just needs to pull simple reports from the database.

For those developers who are used to traditional relational databases such as MySQL and PostgreSQL, you can think of the aggregation framework as a way to preform GROUP BY operations.

Time to live collections

This feature allows you to create a collection where the mongod daemon will automatically remove the data after specified period of time. This would be extremely useful if your application stores audit trails of users activity that is only relevant for a given period of time. This TTL feature is implemented as a special index type on the collection that keeps track of insertion time of a document.

Getting started with MongoDB 2.2 on OpenShift

Step 1: Create an OpenShift Account

If you don’t already have an OpenShift account, head on over to the website and signup. It is completely free and Red Hat gives every user three free Gears on which to run your applications. At the time of this writing, the combined resources allocated for each user is 1.5 GB of memory and 3 GB of disk space.

Step 2: Install the client tools on your machine

Note: If you would rather watch a screencast of this step, check out the following videos where I demo how to install the client tools.

Windows

Linux Ubuntu

Linux Fedora

OSX

The OpenShift client tools are written in a very popular programming language called Ruby. With OSX 10.6 or later and most Linux distributions, ruby is installed by default so installing the client tools is a snap. Simply issue the following command on your terminal application:

sudo gem install rhc

Step 3: Create an OpenShift application

Now that we have an OpenShift account and the client tools installed, lets get started with MongoDB. The first thing we need to do is create a gear that will house our application code and database. I will be using the command line tools that we installed in step 2, but you can perform the same action via the web console or using our IDE integration. I will also be using the PHP 5.3 application type but MongoDB 2.2 will work with any application type that you prefer.

$ rhc app create mymongodb php-5.3

This will create an application container for us, called a gear, and setup all of the required SELinux policies and cgroup configuration. OpenShift will also setup a private git repository for you and propagate your DNS out world wide. This whole process should take about 30 seconds.

Step 4: Add MongoDB 2.2 to your application

Now that we have our application gear created, let’s add MongoDB 2.2 support. We do this by embedding a cartridge into our application. The syntax for this is as follows:

$ rhc cartridge add mongodb-2.2 -a mymongodb

Once this command is finished, you will be provided with the admin username and password for your database.

Note: You have the admin username for this instance of MongoDB and full permissions to create new databases, collections, users etc. This is not a shared database.

Step 5: Use your database

At this point, you can use the port-forwarding feature of OpenShift to connect to your MongoDB database from your local machine, or you can ssh into your OpenShift gear and access the database using mongoshell.

Once you have connected to your OpenShift gear, simply issue the following command to connect to your mongodb database:

$ mongo

This will drop you into the mongo shell that will allow us to start working.

Step 6: Create a TTL collection

Now that we are connected to our MongoDB database, let’s create a new TTL collection. The following command assumes that you have a collection called audit.events that you want to expire documents from every 7 days.

db.audit.events.ensureIndex({"status":1}, {expireAfterSeconds: 604800})

Next Steps

Now that you have access to the latest features in MongoDB, let us know what you are working on. We would love to showcase your cool app in our OpenShift Developer Gallery to help your application get the attention is deserves. Drop us a line at openshift@redhat.com with your next BIG idea.

Categories
MongoDB
Tags
Comments are closed.