A practical intro to web development with mongo db and nodejs when, why and how v2

38
By Jorge Garifuna Professional Web Developer [email protected] 213-915-4402 JGari.com/resume Twitter: @jgarifuna

Transcript of A practical intro to web development with mongo db and nodejs when, why and how v2

Page 1: A practical intro to web development with mongo db and nodejs  when, why and how v2

ByJorge Garifuna

Professional Web [email protected]

213-915-4402

JGari.com/resume

Twitter: @jgarifuna

Page 2: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

SMS your Name and Email to:

213-985-4413

Page 3: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. A Database that stores data (documents)2. A NoSQL Database

1. NoSQL = Not only SQL3. Uses JSON for interaction

1. JSON = JavaScript Object Notation4. Managed by 10gen company

Page 4: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. Scalable2. High performance3. Open source4. Written in C++5. Humongous

Page 5: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. Document-Oriented Storage2. Full Index Support3. Replication & High Availability: Mirror across

LAN/WAN4. Auto-Sharding: Scale horizontally5. Map/Reduce: aggregation & data processing6. GridFS: Store files of any size

Page 6: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. Not a Relational Database (RDBMS)2. Not ideal for every scenario

Page 7: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

id firstName

lastName

age

1 Jorge Garifuna 85

2 Jimmy Smith 30

id: 1firstName: JorgelastName: Garifunaage: 85

id: 2firstName: JimmylastName: Smithage: 30

id: 3firstName: AlanlastName: Jonesage: 25city: Los Angelesstate: CA

Relational Database Table/Records MongoDB Collection/Documents

Page 8: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. I dig alpha products2. Beta products are my cut of tea3. I only consider stable products

Page 9: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Credit: Sanjeev Mishra

Page 10: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. When you need flexibility in your data

2. When you want to easily scale3. When your dataset does not

have zillions of joins

Page 11: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Page 12: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. Download from 1. http://www.mongodb.org/downloads

2. Unzip package3. Run the following from terminal

1. sudo mkdir -p /data/db2. sudo chown `id -u` /data/db

4. Add to path1. Mac example: export PATH=/Users/jgarifuna/jg/net/Dev/db/mongodb-osx-x86_64-2.2.1-rc0/bin:$PATH

2. Linux example: PATH=/home/jgarifuna/mongo-linux-2.2.1/bin:$PATH

Page 13: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. If added to path in command line type

1. mongod2. If not on path

1. Access the bin folder on mongo folder2. Type: ./mongod

Page 14: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. If added to path in command line type

1. mongo2. If not on path

1. Access the bin folder on mongo folder2. Type: ./mongo

Page 15: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. Databases are created automatically

2. Tables are created automatically3. Primary key is created

automatically

Page 16: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Page 17: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Page 18: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Page 19: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Page 20: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Page 21: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Page 22: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/Replica+Sets

Page 23: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/Replica+Sets+-+Basics

On each mongodb instance start service with: mongod --rest --replSet myset

Page 24: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/Replica+Sets+-+Basics

Connect to primary server: mongo --host PRIMARY_IP_OR_NAME

Initialize replica set: rs.initiate()

If you get error: "local.oplog.rs is not empty on the initiating member. cannot initiate.”

Fix by typing (source: http://stackoverflow.com/questions/10953752/how-to-modify-replica-set-config): ▪ use local▪ db.dropDatabase()▪ Rs.initiate()

Add secondary node to replica set rs.add(‘FIRST_SERVER_NAME_OR_IP’)

Add arbiter node to replica set rs.addArb(‘ARB_SERVER_NAME_OR_IP’)

Page 25: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Source: http://www.mongodb.org/display/DOCS/Replica+Sets+-+Basics

Connect to primary server: mongo --host PRIMARY_IP_OR_NAME

Add a document Switch to your desire db: use DB_NAME db.foo.save({name: “Jorge Garifuna”})

Set secondary to slave (otherwise you wont be able to see data) rs.slaveOk()

Query secondary db.foo.find()

Page 26: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

public static function getIpGeoDataFromMongo($ip) { $rv = array(); if (strlen($ip) > 0) { $m = new Mongo(); // connect, MongoClient $db = $m->selectDB("geoip"); $collection_geo_city_blocks = new MongoCollection($db, self::IP_BLOCKS_TABLE_NAME); $ip = addslashes($ip); $ip = ip2long($ip); $query = array('$and' => array ( array( 'startIpNum' => array('$lte' => $ip) ), array('endIpNum' => array('$gte' => $ip) ) ) ); $cursor = $collection_geo_city_blocks->find($query); $result = $cursor->getNext();

if (isset($result['locId']) && strlen($result['locId']) > 0) { $collection_geo_city_locations = new MongoCollection($db, self::IP_LOCATIONS_TABLE_NAME); $query = array('locId' => $result['locId']); $cursor = $collection_geo_city_locations->find($query); $rv = $cursor->getNext(); } }

return $rv; }

Page 27: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Some Basic Node JS stuff

Page 28: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Platform built on Google Chrome’s JavaScript Runtime

For building fast, scalable network applications

Substitute for Apache/PHP But you create your own server code

Page 29: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

Download from nodejs.org

Run installation

Page 30: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

var http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(1337, '127.0.0.1');console.log('Server running at http://127.0.0.1:1337/');

1. Create new folder2. Create testserver.js file and place within

folder3. Run node

1. Node testserver.js4. On browser go to: http://127.0.0.1:1337

Page 31: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. A Node JS Application Framework2. Uses MVC (model, view, controller)

pattern

Page 32: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. From the command line run Node Package Manager

1. sudo npm install -g express

Page 33: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. From the command line run1. Express ./YOUR_APP_NAME

2. Change into your new app folder1. cd ./YOUR_APP_NAME

3. Install depedencies1. npm install -d

Page 34: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. Change into your new app folder1. cd ./YOUR_APP_NAME

2. Run app with node1. node app

3. On browser go to URL:1. http://localhost:3000

Page 35: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. Change into your new app folder1. cd ./YOUR_APP_NAME

2. Run app with node1. node app

3. On browser go to URL:1. http://localhost:3000

Page 36: A practical intro to web development with mongo db and nodejs  when, why and how v2

JGari.com/resumeSMS your name & email to: 213-985-4413

1. Download workout web api from1. https://github.com/donnfelker/workout-

tracker2. Checkout Develop a RESTful API Using

Node.js With Express and Mongoose1. http://pixelhandler.com/blog/2012/02/09/

develop-a-restful-api-using-node-js-with-express-and-mongoose/

Page 37: A practical intro to web development with mongo db and nodejs  when, why and how v2

While you think… Sign up to LAMPsig’s mailing list at:▪ http://lampsig.org

Join LAMPsig on Meetup at:▪ http://www.meetup.com/LAMPsig

Jorge Garifuna▪ [email protected]▪ @jgarifuna

JGari.com/resumeSMS your name & email to: 213-985-4413

Page 38: A practical intro to web development with mongo db and nodejs  when, why and how v2

1. http://www.mongodb.org2. http://nodejs.org3. http://expressjs.com4. http://pixelhandler.com/blog/

2012/02/09/develop-a-restful-api-using-node-js-with-express-and-mongoose

5. http://lampsig.org6. http://www.meetup.com/LAMPsig

JGari.com/resumeSMS your name & email to: 213-985-4413