A Simple MongooseJs Example

As my latest coding adventure I've decided to give node.js a try.  I read this short little tutorial name the Node Beginner Book.  I went through the entire book in one day.  The book provided me with enough knowledge to be able to produce simple website.   The good accomplished its goal but it did not cover database communications.  I can't think of any business application that does not communicate with some sort of database for storage.

I'd heard that a good choice for using for storage with node.js is MongoDb.  Being that I am totally unfamiliar with MongoDb I did a quick google search and found a driver that can be used to connect node source code to MongoDb.  That driver is called mongoosejs.

So now that I knew all the components to have a database with node I decided to try to connect all the pieces together.  I gotta say that it was not easy for various reasons.  The mongoosejs site was inadequate for me.  It may be totally appropriate for someone who knows the ins and outs of MongoDb, who knows, but for me it was difficult trying to save data and then reading it from their API documentaion.

Another quick google search and it lead me to this video on using mongoose.  That video was quite helpful and it allowed me to see what I was doing wrong.  It allowed me to see that besides installing the mongoose package I needed to install MongoDb, create a database and run it, duh!  How else can you run MongoDb without actually running it.

I am going to list the things that got this demo working and show the entire source code.

Here is a list of the things that I finally did right

1. Install MongoDb - to do this visit Mongo's Website download the appropriate version and install it.  On windows you must also set your PATH variable to point to the bin location for MongoDb.  In my case I had to add C:\Program Files (x86)\mongodb-win32-i386-2.0.6\bin to the PATH variable.  MongoDb downloads as a zip file so you can unzip it and drop it anywhere you want.

2. Installed the mongoose package - to do this visit the mongoose website for information on how to do that.  One thing I will note is that I installed the package in my original node location. Previously I tried doing this and installed the package in my projects root directory and had problems.  To install the package just make sure to open the command line console and navigate to the location where you originally installed node. In my case that is C:\Program Files (x86)\nodejs.  This will drop the package at the following location C:\Program Files (x86)\nodejs\node_modules\mongoose.

3. Run MongoDb.  To do this run the command  mongod -dbpath ./data/ (make sure to create the data directory first though).  This will create a database in wherever folder you are in the command line.  Once you do that you are up and running.

The example below is all you need to get a picture of what it takes to connect to the database, save some data.  Then later in the display it fetches that data.  Like I said this is the simplest example I could come up with to get me talking to the database.  Once you do that you can move on to real world scenarios like actually receiving data from user input and displaying in grids, possibly editing the data as well.

  I will do my best to give a short and quick description of what is going on in the code snippet above. I also plan to push the entire source up to https://github.com/fernandozamoraj so please look up there for the full running demo.

To run the full demo you must first startup the application by typing node index.js at the command line. Then just type http://localhost:8877/save and then http://localhost:8877/display. The first URL is to save the data and the second is to display it to the console. It does not display it to the browser so be warned.

The save function does the following :

  1. Connects to a database
  2. Creates a schema for the TodoModel
  3. Creates and item of the TodoModel
  4. Saves the item
  5. Disconnects from the database

The display function does the following:

  1. Connects to the database
  2. Creates a schema for the TodoModel - Not sure why we have to create it each time
  3. Queries the database by the key
  4. Displays the entire document to the console
  5. Displays the documents attributes: key, item, description
  6. Disconnects from the database

There you have it. If you have any questions please feel free to leave me a comment.


Decor said…
Really helpful thanks!

Popular posts from this blog

Simple Example of Using Pipes with C#

Difference Between Adapter and Bridge Design Patterns

Remote Access to Your Home Desktop Using No-IP