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. Thats 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
Inserts Take Less Than a Millisecond}</p>
<p>Testing the code in the browser the very first time was a bit slow (about 1,000 milliseconds), because the collection had to first be created. But upon refreshing the browser window I saw that the collection w
;
collection.findOne(search, function(err, doc) {
In this case, one field is a string and the other is a regular expression. Without an index, it took more than 4,000 milliseconds (4 seconds) to find. I used the mongo//DESK: leave lowercase// shell to add an index. Subsequently, the query time dropped to around 0.6 milliseconds. This, of course, is a feature of MongoDB itself, but it showed how quickly a retrieval can run using the Node.js driver, as long as the database is configured properly.
In addition to the usual database operations such as inserting and querying, the MongoDB driver also includes administration functionality, which I could have used to write system administration utilities. This functionality includes adding, removing and authenticating users; enumerating the databases and collections; setting profiling levels; and so on.
Finally, one item thats notably missing from the driver is high-level object modeling. This is by design, as its meant to be a low-level driver that only covers direct MongoDB features. If youre looking for modeling, I recommend using this driver in conjunction with the Mongoose library.