NoSQL database engines such as MongoDB are pretty trendy at the moment. Every programmer I've talked to in the last couple of years seems to want to work with one of the NoSQL database engines! The argument most often cited in favor of using this technology is enhanced scalability. I'm not so sure about the scalability issue in all cases, but NoSQL technologies definitely have some solid use cases, as opposed to those of relational databases.

Aside from the desire of programmers (myself included) to work with the latest technologies, there are certain reasons for using MongoDB and other NoSQL products. As with most things in IT, language selection often comes down to cost. Lately I've noticed a move toward using more lightweight technologies in an effort to simplify IT development and maintenance.

My articles "Protect C++ Legacy Programs by Using Python" and "Exception Management in C++ and Python Development: Planning for the Unexpected" discuss programming languages at some length, focusing on Java and Python. One theme of these articles is the relative ease with which Python code can be developed, as compared to Java code. Of course, Java solutions are more heavyweight, bringing benefits such as out-of-the-box thread-safety, strong typing, and security. By contrast, in many cases Python allows you to get to a first-draft prototype much more quickly, which is one reason why many organizations are now building prototypes using Python. The Python prototype is tested and then replaced by an implementation in Java (or whatever is the preferred mainstream high-level language).

The allied area of databases is another key element in the IT cost base. License costs aren't really the issue; high-performance, open source database engines such as MySQL are widely available and have been proven over many years of use. Rather, key costs have more to do with software development and maintenance expenses.

TIP: For more on multi-coding, be sure to check out Stephen's blog "Coding in the Multi-Language Era."

Installing and Running MongoDB

With these issues in mind, let's see how the interesting combination of MongoDB and Python stacks up. As always, a good place to start is installing and playing around with a test system. I used Ubuntu 12.04.4 LTS for the examples in this article, but that setup isn't mandatory.

Getting MongoDB up and running is pretty straightforward: Just run the five commands in Listing 1 to install and start MongoDB.

Listing 1-Installation and startup for MongoDB.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart
 dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-org
sudo service mongod start

The last command in Listing 1 may actually be superfluous, because the installation starts the service by default. I include the command just in case the engine doesn't start automatically.

To verify that MongoDB is running, use this command:

/etc/init.d/mongod status
    

This command should produce a status message with content similar to that in Listing 2.

Listing 2-MongoDB is running.

Rather than invoking init scripts through /etc/init.d, use the service(8)
    
utility, e.g. service mongod status
    
Since the script you are attempting to invoke has been converted to an
    
Upstart job, you may also use the status(8) utility, e.g. status mongod
    
mongod start/running, process 11969
    

TIP: If you prefer, you can use this Upstart equivalent to verify that MongoDB is running:

service mongod status

If all has gone well with the installation, you should now have a working MongoDB installation. Nice and easy!

Now let's try to use the database engine. The following examples are based on those in the MongoDB online manual.

Connecting to MongoDB

It's easy to connect to MongoDB: Just type this:

mongo

This command should launch the MongoDB shell and display the text shown in Listing 3 (or something similar):

Listing 3-Connecting to MongoDB.

MongoDB shell version: 2.6.3
    
connecting to: test
    
Server has startup warnings:
    
2014-07-30T16:34:05.028+0100 [initandlisten]
    
2014-07-30T16:34:05.028+0100 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
    
2014-07-30T16:34:05.028+0100 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
    
2014-07-30T16:34:05.028+0100 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
    
2014-07-30T16:34:05.028+0100 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
    
2014-07-30T16:34:05.028+0100 [initandlisten]
    
>

In Listing 3, notice that the very last line provides the console prompt (>) for interacting with MongoDB. In the remainder of this article, I'll describe some commands that can be typed at the console. To start using the console, display the current (default) MongoDB database by using this command:

db

This command produces the following output:

> db
    
test

To view all databases, use this command:

> show dbs
    
admin (empty)
    
local 0.078GB
    
mydb 0.078GB
    
testData 0.078GB

NOTE: Users of MySQL might recognize some similarity to the MongoDB commands. I was struck by this resemblance the first time I used Mongo.