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 
Installing MongoDB/node.js and Hello Worlds
Notes taken on June 5, 2014 by Edward Tanguay
installing MongoDB on Mac
mongodb.org/downloads
always use 64-bit versions
32-bit are only suitable for development
they limit total amount of data to 4 GB
stable releases are always even, e.g. 2.6.0
terminal window
tar xvf mongodb-osx-x86_64-2.6.0.tgz
cd into it
bin where the action is
mongo is shell to connect database
mongod runs the server
sudo bash
mkdir -p /data/db
chmod 777 /data/db
not good for production of course
start mongo server
./mongod
starts up
listening on port 27017 (default)
start mongo shell
./mongo
test
db.names.insert({'name':'Joe'})
db.names.find()
installing on Windows
mongodb.org/downloads
always 64-bit if you can
find out:
powershell
wmic os get osarchitecture
130 MB
in Program Files
server:
mongod.exe
\data\db does not exist
create directories
client
new window
mongo.exe
db.names.insert({'name':'Joe'})
db.names.find()
introduction to Mongo shell
show dbs
use demo (creates it since it doesn't exist)
db.things.insert({'a':1, 'b': 2, 'c': 3})
db.things.insert({'d':4, 'e': 5})
db.things.find({'a':1})
we are in a complete javascript environment, so we can do anything that is supported by the V8 javascript engine
for (var i = 0; i <= 10; i++) { db.things.insert({'x':i})}
JSON
basic structure
human readable format
key/value pairs
ObjectId field unique across system
value types
many different value types
you can have same keys with different types
basic nesting
nested array: 'colors' : ['red','black']
nested object (with attributes with key/values)
deep nesting
students with names and activities
you can show it like a javascript object
and modifty it as a json object
installing node.js
install on Mac
nodejs.org
click install
run through it
test
node
console.log("hello world");
install on windows
nodejs.org
download, install
go to: C:\Program Files\nodejs\
node.exe
console.log('hello world');
asynchronous vs. synchronous
two ways of handling operations that don't require active work on the currently running thread or process
usually these are referring to IO
mongo shell is synchronous while node.js is asynchronous
mongo shell
var doc = db.coll.findOne();
printjson(doc);
node.js
overhead code to connect to db
not getting a return value but passing a callback function, passing an err and document value
...function(err,doc) {
db.close();
advantage of asynchronous is that it can handle a large number of users
the mongo shell blocks other users until the values come back from the db
run it:
mongo script.js
node app.js
helloworld server in node.js
console.log('testing');
now make web server
var http = require('http');
var server = http.createServer(function (request, response) {...
then http://localhost:8000/
see: howtonode.org/hello-node