EDWARD'S LECTURE NOTES:
More notes at http://tanguay.info/learntracker
C O U R S E 
MongoDB for Node.js Developers
Andrew Erlichson, MongoDB University
https://university.mongodb.com/courses/10gen/M101JS/2014_June/about
C O U R S E   L E C T U R E 
MongoDB, node.js, Express and SWIG
Notes taken on June 9, 2014 by Edward Tanguay
npm
package manager for node.js
node app.js
cannot find package
express = require('express') in app.js
so we have do npm install express
also requires express and mongodb
we could install these indivually or with package.json
we just have to say npm install
installs them in node_modules directory
you could also install them globally
helloworld in mongodb
node.js driver
communicates with mongod with BSON, a binary jSON
driver is written in javascript
handles things behind the scenes
how to install
node app.js
cannot find module mongodb
installs mongodb module
var mc = require('mongodb').MongoClient;
access the driver
pass in a callback, it calls it when done connecting
helloworld in express
takes care of routing and details of web application for us
callback takes two arguments
app.get has respond and request in callback
express server started on port 8080
in app.get, the star is wildcard for any handler that is not handled
extending helloworld in express
make file views/hello.html
<h1>Hello, {name}</h1>
templating engine: consolidate
a set of wrappers for template libraries for express
app.engine('html',cons.swig);
cons is consolidate and swig is the templating engine
helloworld with mongodb, express, and swig
adding calls to mongo db from the last example
Server = require('mongodb').Server;
told it to use the native BSON parser
not registering a callback
it looks like we are using our database connection before it is even connected
but we actually start listening as soon as the database connection is open
we are just setting up mongoClient as an object and then pass it as a parameter in the callback
express: handling GET requests
registering only one route
app.get('/:name', ...
uses this template
express: handling POST requests
form to pick radio button choice
we have app.get and app.post
express.bodyParser() populates req.body so we can access it
error handling function
call next with an error object
mongodb is schemaless
in RDBMS if you want to add a new field to a row, you need to create a new column for all rows in the table
you don't need to do this in mongodb
this allows mongodb to be very agile
JSON
there are only two data structures inside JSON
arrays
[...]
dictionaries
{ key : value, key : value }
blog project
database model if we used RDBMS
database model in mongodb
you can embed the comments in the posts
if the email is missing from any comment, it doesn't matter, you can leave it out
it is the way you access the data that guides how to design your data structures