Using MongoDB with Express

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

Starting with the code we did in the last tutorial, we will now demonstrate using MongoDB with Express on Node.js.

1. First we had set up a mongolab database called “firstdb” with a collection called “highlowtemp” with data structured as shown…

mongolab database

mongolab database

We have 50 records, one record for the high and low temperatures of each United States.

2.  In package.json, we specify that we depend on mongodb …

depends on mongodb in package json file

depends on mongodb in package json file

3. So in our node.js application’s main app.js file, we can require the use of mongodb.

main app file

main app file

 

We also made a few changes to the app.js from previous.  In particular, we are passing the string “results here” to our “record” placeholder field in our home template.

4.  Our home template looks like this …

home page template

home page template

In the screen shot you also see our directory structure.  See how the {{record}} placeholder is suppose to be filled with the result of pulling an Alaska temperature record.

5. In our app.get of root, we now make a call to connect to Mongodb…

connect to mongodb in nodejs

connect to mongodb in nodejs

The connection  URI can be found from Mongodb dashboard.  Replace “dbuser” with the database username.  Use its password instead of “secret”.  And mongodb should give you the “host” and “port” numbers.  “firstdb” is the database that we are connecting to.

4.  Run “node app.js” and point your browser to http://localhost:3000 to see if things are still working.

5. We will query into the “highlowtemp” collection looking for record for the state of Alaska.  Since we know that our collection just have one record for this state, we will use “findOne”.  Add this bit of code and run to see that we retrieve the Alaska record and display to the console …

findOne record

findOne record

Note that you have to hit the server with your browser by http://localhost:3000 in order for it to run the query.

6.  The variable query is the query object with key value pair.  key being the field we are looking for with the value of ‘Alaska’.  db was the database that was returned to us from MongoClient.connect.  We use this db object to get into our highlowtemp collection by …

db.collection(‘highlowtemp’)

And then we do a findOne() on that collection.  findOne() wants an query object and returns an error (“err”) and document (“doc”) in its callback function.

If query is successful, err will not be true and the “doc” object will contain our result.  Finally, we close the connection to the database with db.close();

7.  But instead of outputting result to console, we want to pass to our view.  Our view needs a single data value.  Instead of a dummy string, we pass it “doc.high.fahrenheit” which the high temperature of Alaska in Fahrenheit.  We know the structure of the doc object because we had output it with console.log above.

pull data from doc

pull data from doc