Mongo DB

Integrating Mongo and Node

If you’ve followed the last two posts you should have Node.js and Express as well as Mongo DB installed.  The next step is to install the Mongo driver in your application’s dependencies and start using the database within your app.

Let’s go!

    1. First install the Mongo db driver for Node.  The easiest way it to add it to your app’s dependencies.   Navigate to the myApp directory you created earlier and open the ‘package.json’ file in a text editor.  Add the following:
        "name": "myApp",
        "version": "0.0.1",
        "private": true,
        "scripts": {
          "start": "node app.js"
        "dependencies": {
          "express": "3.4.4",
          "jade": "*",
          "mongodb": "*"
    2. Save the file.
    3. Now rebuild the app dependencies.  Open a terminal window and navigate to your myApp directory.  Type the following:
      $npm install
    4. You may see some warnings.  I did, but they were only warnings.   If you are concerned you can review the build logs in the myApp/node_modules/mongodb/node_modules/<bson or kerberos>/builderror.log files.   Happy?  Ok, now let’s modify the app to connect to the database. Open app.js in a text editor and add the following:
      app.get('/mongo', function(req, res){
          var MongoClient = require('mongodb').MongoClient, 
            format = require('util').format;
            function (err, db) {
              if (err) {
                  throw err;
              } else {
                  console.log("successfully connected to the database");
    5. Save the file. Ensure your Mongo DB is up and running using mongod command. Then start the Node server by typing:
      $ node app
      Express server listening on port 3000
    6. Now open a browser and enter the test url: localhost:3000/mongo. The terminal console should read:
      successfully connected to the database
    7. If so, great! If not, boo. Go back and see what you missed.
    8. At this point the basic structure is in place to start building our database app. We’ll do that in a future post.

How to install and setup Mongo DB in a minute or less

In my previous post I showed you how to install Node.js and Express.  Now we’re going to install Mongo DB.   If you’re unfamiliar with Mongo, it is a schema-less, JSON-based database that is leading the whole noSQL movement and will soon overtake DB2 in terms of popularity (sorry IBM).

Ready to go?

    1. If you followed my previous post to setup Node then your environment should be good to go. If not, go back and make sure you have Brew installed and it is up to date.  Open a terminal and type:
      $ brew install mongodb
    2. That’s it. Well except for some configuration.  Let’s do those steps now.  First create a default directory for your databases.  You’ll need root access to do this, in the terminal type:
      $ sudo mkdir -p /data/db
    3. And change permissions on the new directory:
      $ sudo chown 'id -u' /data/db
    4. Now start Mongo.
      $ mongod
      all output going to: /usr/local/var/log/mongodb/mongo.log
    5. Done, done and done.