Getting Started with MongoDB Shell on OpenShift

Last week, we announced and demonstrated MongoDB on OpenShift. One thing that experienced Mongo users immediately asked was “How do I get to the Mongo Shell?” In this post, I will show you how.

First, let’s create an application. I’m going to name this application “zarkov”, in honor of the scientist who explored the planet Mongo.

Run the command

  $ rhc-create-app -a zarkov -t wsgi-3.2

which displays

Application Info
    Framework: wsgi-3.2
     Creation: 2011-12-16T18:45:34-05:00
         UUID: 4036e1eb9f114c278d5f9aaf92dda2d9
      Git URL: ssh://4036e1eb9f114c278d5f9aaf92dda2d9& @ \

Make a note of the UUID and host, because we will need it later. In this case they are “4036e1eb9f114c278d5f9aaf92dda2d9” and “”.

And now embed MongoDB in it.

  $ rhc-ctl-app -a zarkov -e add-mongodb-2.0

which gives us

MongoDB 2.0 database added.  Please make note of these credentials:
       Root User: admin
   Root Password: c3XBw52WlM5n
   Database Name: zarkov
  Connection URL: mongodb://
You can manage your new MongoDB by also embedding rockmongo-1.1

Save that password, we will need it later. In this case, it’s “c3XBw52WlM5n”, but it will be different each time you create a new application.

Now we connect to the OpenShift shell and have it run the Mongo shell

  ssh -t \
    -l 4036e1eb9f114c278d5f9aaf92dda2d9 \ \
      rhcsh mongo

The uuid based “username” and hostname were what we saved earlier after creating the app. The “-t” option to ssh forces the far side to create a tty, so features such as command line editing work correctly.

Now we’re at the Mongo shell prompt, and you can issue any Mongo commands or JavaScript code you care to enter.

First we have to authenticate

use admin
db.auth("admin", "c3XBw52WlM5n");

That was the MongoDB admin password we saved earlier.

Now switch to the application database. The OpenShift Mongo cartridge creates a database for the application, and gives it the same name as the application. So we enter

use zarkov

To look at the statistics for this database, now enter


which will display

"db" : "zarkov",
"collections" : 4,
"objects" : 9,
"avgObjSize" : 52.888888888888886,
"dataSize" : 476,
"storageSize" : 20480,
"numExtents" : 4,
"indexes" : 2,
"indexSize" : 16352,
"fileSize" : 16777216,
"nsSizeMB" : 16,
"ok" : 1

What if we want to access the Mongo Shell in a batch manner, as part of a UNIX pipeline, instead of interactively?

Create in input script named “input.text” that looks like this

use admin
db.auth("admin", "c3XBw52WlM5n");
use zarkov

And then run the following

ssh \
  -l 4036e1eb9f114c278d5f9aaf92dda2d9 \ \
  rhcsh mongo \

and the database stats JSON will be output. Notice that there is no “-t” option to ssh.

Here is an advanced trick: What if we lose the MongoDB admin password?

Run the following command

  ssh \
    -l 16edf15b6456415aac02007ec84eedf6 \ rhcsh \

and it will be displayed.

Want to see all this goodness in action? Then sign up for our FREE webinar with 10Gen on Dec 20th.

As always, if you have any questions, feel free to ask them on the OpenShift Forums or come to IRC at and ask on the #openshift channel.

Comments are closed.