MongoDB Driver Delivers Effective Support for Node.js

In this review, eWEEK Labs takes a look at the features and performance of the MongoDB driver for Node.js and finds that experienced developers will benefit from the 1.0 release.

On April 25, 10gen released the first official MongoDB driver for Node.js. Although this is the first official release, the open-source MongoDB driver for Node.js has been in development and early release for more than two years. The new driver works well and provides full access to nearly all MongoDB features that work within a JavaScript program that is running under Node.js.


To see images of MongoDB in action, click here

Working with version 1.0.0, eWEEK Labs took a comprehensive look at the main features that make up the MongoDB driver for Node.js, including available database features, the driver API, and overall performance based on how fast the calls were made.

I tested the MongoDB driver on a Rackspace Ubuntu 11.04 server, running Node.js with the Express framework. For timing, I used the node-microtime module. For source-code editing, I used the Cloud9 integrated development environment (IDE), but to run the applications, I used the bash shell directly.

As with most Node modules, installation was quick and smooth with the Node Package Manager (NPM) installer. I chose to install the packages globally using the €œg€ option of NPM. After creating the initial express project, I linked the package to the project with the NPM link command.

The MongoDB driver for Node.js supports nearly all the operations that MongoDB itself supports. I tested many of them, starting with a basic insert operation. Because I worked in Node.js, which is an implementation of JavaScript, I was able to define my objects in precisely the same notation that MongoDB uses. While I was at it, I included timing code so that I could test how fast it is. I put this code inside an HTTP Get operation.

The code makes heavy use of the Node.js approach of asynchronous callback functions so that the code does not block while the database operations are performing. I opened the database and passed in a function that gets called after the database is opened. That€™s the first callback and is also where I retrieve the collection.

This callback approach can be a little confusing to less-experienced coders. The idea is that Node.js is fully asynchronous and nonblocking; as such, I passed in a function that gets called only after the database is open. Meanwhile, the rest of the other function continues on its way, and depending on how long the I/O takes, likely finishes before the database operations are even complete. This is the norm for Node.js programming and is the approach upon which the entire library is built.

Typically, MongoDB programming, standard database operations (create, insert, update and delete) run on a collection object. IT developers who have used a MongoDB driver in another language (such as C# or Ruby) should have no trouble applying these same operations using the Node.js driver