MongoDB and Node.js
-
Upload
norberto-leite -
Category
Software
-
view
2.231 -
download
0
Transcript of MongoDB and Node.js
![Page 1: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/1.jpg)
MongoDB + Node.js
Building first app with MongoDB and Node.js
![Page 2: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/2.jpg)
2
Agenda
MongoDB + Node.js Driver ODM's MEAN Stack Meteor
![Page 3: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/3.jpg)
3
Ola, I'm Norberto!
Norberto Leite Technical Evangelist Madrid, Spain @nleite [email protected] http://www.mongodb.com/norberto
![Page 4: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/4.jpg)
MongoDB Node.js
![Page 5: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/5.jpg)
INFACT
![Page 6: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/6.jpg)
MongoDB JavaScript
![Page 7: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/7.jpg)
7
Few reasons why
Flexible Agile Web Language
![Page 8: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/8.jpg)
8
MongoDB + Javascript
• MongoDB Shell – JS interperter
• MongoDB MapReduce – Runs on top of V8 – Map and Reduce functions are JS functions
• Native support for Node.js – One of the most used Drivers out there! – https://www.npmjs.com/package/mongodb
![Page 9: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/9.jpg)
Node.js
![Page 10: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/10.jpg)
10
2 Foundations
Events Streams
![Page 11: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/11.jpg)
11
2 Foundations
• Events / Event Loop – Single Thread Applications – No threads – Events Emitter – Event Queue – Known Events
• Streams – Read, Write, Both – Unix Pipes – We use it extensively!
![Page 12: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/12.jpg)
Install
![Page 13: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/13.jpg)
npm package
$ npm install mongodb
![Page 14: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/14.jpg)
Compatibility
http://docs.mongodb.org/ecosystem/drivers/node-js/#compatibility
![Page 15: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/15.jpg)
15
Compatibility w/ MongoDB
![Page 16: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/16.jpg)
Initialize Project
![Page 17: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/17.jpg)
package.json file
$ mkdir firstappnodejs $ cd firstappnodejs $ npm init
![Page 18: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/18.jpg)
package.json file $ mkdir firstappnodejs $ cd firstappnodejs $ npm init ... { "name": "firstappnodejs", "version": "0.0.1", "description": "Small demo webinar application", "main": "index.js", "scripts": { "test": "workitout" }, "repository": { "type": "git", "url": "git://github.com/nleite/firstappnodejs" }, "dependencies": { "mongodb": "~2.0" }, "keywords": [http://docs.mongodb.org/ecosystem/drivers/node-js/#compatibility "demo", "nodejs", "mongodb" ], "author": "Norberto Leite", "license": "Apache 2.0", "bugs": { "url": "https://github.com/nleite/firstappnodejs/issues" }, "homepage": "https://github.com/nleite/firstappnodejs" }
![Page 19: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/19.jpg)
package.json file $ mkdir firstappnodejs $ cd firstappnodejs $ npm init ... { "name": "firstappnodejs", "version": "0.0.1", "description": "Small demo webinar application", "main": "index.js", "scripts": { "test": "workitout" }, "repository": { "type": "git", "url": "git://github.com/nleite/firstappnodejs" }, "dependencies": { "mongodb": "~2.0" }, "keywords": [ "demo", "nodejs", "mongodb" ], "author": "Norberto Leite", "license": "Apache 2.0", "bugs": { "url": "https://github.com/nleite/firstappnodejs/issues" }, "homepage": "https://github.com/nleite/firstappnodejs" }
![Page 20: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/20.jpg)
Install our new firstappnodejs app!
$ npm install > [email protected] install … … > [email protected] install > [email protected] node_modules/mongodb ├── [email protected] ([email protected], [email protected], [email protected], [email protected]) └── [email protected] ([email protected], [email protected]) firstappnodejs/ $ ls node_modules package.json
![Page 21: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/21.jpg)
Connect
![Page 22: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/22.jpg)
boot up MongoDB Server
$ mkdir ~/firstappdb $ mongod --dbpath ~/firstappdb
![Page 23: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/23.jpg)
boot up MongoDB Server
$ mkdir ~/firstappdb $ mongod --dbpath ~/firstappdb --auth --keyfile ~/n.pem
https://www.mongodb.com/products/mongodb-enterprise-advanced
![Page 24: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/24.jpg)
boot up MongoDB Server
$ mkdir ~/firstappdb $ mongod --dbpath ~/firstappdb --auth --keyfile ~/n.pem
https://www.mongodb.com/products/mongodb-enterprise-advanced
![Page 25: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/25.jpg)
var MongoClient = require('mongodb').MongoClient, assert = require('assert');
Connect
![Page 26: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/26.jpg)
var MongoClient = require('mongodb').MongoClient, assert = require('assert'); //connection uri var uri = "mongodb://localhost:27017/firstapp"
Connect
![Page 27: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/27.jpg)
var MongoClient = require('mongodb').MongoClient, assert = require('assert'); //connection uri var uri = "mongodb://localhost:27017/firstapp" //connect to MongoDB MongoClient.connect(uri, function(err, db){ assert.equal(null, err); console.log("Connected correctly to server"); db.close(); });
Connect
![Page 28: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/28.jpg)
28
Connection Pooling
• No traditional Pooling mechanism – Single thread process
• Sockets to pipeline operations • Failover
– Buffering up operations – bufferMaxEntries – numberOfRetries – retryMiliSeconds
http://mongodb.github.io/node-mongodb-native/2.0/api/Db.html
![Page 29: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/29.jpg)
CRUD
![Page 30: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/30.jpg)
var insertDocuments = function(db, cb){ //we don't need to explicitly create a collection var collection = db.collection('myCollection'); collection.insertMany([ {"mongodb": "is just awesome"}, {"nodejs": "so awesome"} ], function(err, result){ assert.equal(null, err); //inserted 2 documents assert.equal(2, result.insertedCount); //invoke callback cb(result); }); }
Insert
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#insert
![Page 31: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/31.jpg)
var insertDocuments = function(db, cb){ //we don't need to explicitly create a collection var collection = db.collection('myCollection'); collection.insertMany([ {"mongodb": "is just awesome"}, {"nodejs": "so awesome"} ], function(err, result){ assert.equal(null, err); //inserted 2 documents assert.equal(2, result.insertedCount); //invoke callback cb(result); }); }
Insert
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#insert
![Page 32: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/32.jpg)
var insertDocuments = function(db, cb){ //we don't need to explicitly create a collection var collection = db.collection('myCollection'); collection.insertMany([ {"mongodb": "is just awesome"}, {"nodejs": "so awesome"} ], function(err, result){ assert.equal(null, err); //inserted 2 documents assert.equal(2, result.insertedCount); //invoke callback cb(result); }); }
Insert
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#insert
![Page 33: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/33.jpg)
var insertDocuments = function(db, cb){ //we don't need to explicitly create a collection var collection = db.collection('myCollection'); collection.insertMany([ {"mongodb": "is just awesome"}, {"nodejs": "so awesome"} ], function(err, result){ assert.equal(null, err); //inserted 2 documents assert.equal(2, result.insertedCount); //invoke callback cb(result); }); }
Insert
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#insert
![Page 34: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/34.jpg)
MongoClient.connect(uri, function(err, db) { assert.equal(null, err); console.log("Sweet! Talking to Server"); insertDocuments(db, function() { db.close(); }); });
Insert
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#insert
![Page 35: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/35.jpg)
MongoClient.connect(uri, function(err, db) { assert.equal(null, err); console.log("Sweet! Talking to Server"); insertDocuments(db, function() { db.close(); }); });
Insert
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#insert
![Page 36: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/36.jpg)
var updateDocument = function(db, cb){ var collection = db.collection("myCollection"); collection.updateOne( {"mongodb": "is just awesome"}, {$set: {"users": ["nleite"]}}, function( err, result){ assert.equal(null, err); assert.equal(1, result.modifiedCount); console.log("Cool, just updated"); cb(result); }); }
Update
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#update
![Page 37: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/37.jpg)
var updateDocument = function(db, cb){ var collection = db.collection("myCollection"); collection.updateOne( {"mongodb": "is just awesome"}, {$set: {"users": ["nleite"]}}, function( err, result){ assert.equal(null, err); assert.equal(1, result.modifiedCount); console.log("Cool, just updated"); cb(result); }); }
Update
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#update
![Page 38: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/38.jpg)
var updateDocument = function(db, cb){ var collection = db.collection("myCollection"); collection.updateOne( {"mongodb": "is just awesome"}, {$set: {"users": ["nleite"]}}, function( err, result){ assert.equal(null, err); assert.equal(1, result.modifiedCount); console.log("Cool, just updated"); cb(result); }); }
Update
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#update
![Page 39: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/39.jpg)
var updateDocument = function(db, cb){ var collection = db.collection("myCollection"); collection.updateOne( {"mongodb": "is just awesome"}, {$set: {"users": ["nleite"]}}, function( err, result){ assert.equal(null, err); assert.equal(1, result.modifiedCount); console.log("Cool, just updated"); cb(result); }); }
Update
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#update
![Page 40: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/40.jpg)
MongoClient.connect(uri, function(err, db) { assert.equal(null, err); console.log("Ok, I can now update!"); updateDocuments(db, function() { db.close(); }); });
Update
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#update
![Page 41: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/41.jpg)
Remove
var removeDocument = function(db, cb){ var collection = db.collection("myCollection"); collection.deleteOne( {"users": "nleite"}, function( err, result){ assert.equal(null, err); assert.equal(1, result.deletedCount); console.log("purged the @nleite contaminated data!"); cb(result); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#remove
![Page 42: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/42.jpg)
Remove
var removeDocument = function(db, cb){ var collection = db.collection("myCollection"); collection.deleteOne( {"users": "nleite"}, function( err, result){ assert.equal(null, err); assert.equal(1, result.deletedCount); console.log("purged the @nleite contaminated data!"); cb(result); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#remove
![Page 43: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/43.jpg)
Remove
var removeDocument = function(db, cb){ var collection = db.collection("myCollection"); collection.deleteOne( {"users": "nleite"}, function( err, result){ assert.equal(null, err); assert.equal(1, result.deletedCount); console.log("purged the @nleite contaminated data!"); cb(result); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#remove
![Page 44: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/44.jpg)
Remove
MongoClient.connect(uri, function(err, db) { assert.equal(null, err); console.log("Ok, I can now delete!"); removeDocuments(db, function() { db.close(); }); });
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#remove
![Page 45: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/45.jpg)
Find
var findAllDocuments = function(db, cb){ var collection = db.collection('myDocuments'); //or collection.find() collection.find({}).toArray(function(err, docs){ assert.equal(err, null); assert.equal(1, docs.length); console.log("Gotcha! found "+ docs.length); console.dir(docs); cb(docs); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#find
![Page 46: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/46.jpg)
Find
var findAllDocuments = function(db, cb){ var collection = db.collection('myDocuments'); //or collection.find() collection.find({}).toArray(function(err, docs){ assert.equal(err, null); assert.equal(1, docs.length); console.log("Gotcha! found "+ docs.length); console.dir(docs); cb(docs); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#find
![Page 47: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/47.jpg)
Find
var findAllDocuments = function(db, cb){ var collection = db.collection('myDocuments'); //or collection.find() collection.find({}).toArray(function(err, docs){ assert.equal(err, null); assert.equal(1, docs.length); console.log("Gotcha! found "+ docs.length); console.dir(docs); cb(docs); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#find
![Page 48: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/48.jpg)
Flexibility
![Page 49: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/49.jpg)
Schema Flexibility
![Page 50: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/50.jpg)
Different Schemas
var insertDifferentShapes = function(db, cb){ var doc1 = {"name": "Norberto", "talks": [ {"nodejs":10}, {"java":15}, "python":11]}; var doc2 = {"name": "Bryan", "webinars": 30}; var coll = db.collection("content") coll.insertMany( [doc1, doc2], function(err, result){ assert.equal(err, null); assert.equal(2, result.insertedCount); console.log("Sweet, inserted "+ result.insertedCount); cb(result); }); }
http://docs.mongodb.org/manual/data-modeling/
![Page 51: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/51.jpg)
Different Schemas
var insertDifferentShapes = function(db, cb){ var doc1 = {"name": "Norberto", "talks": [ {"nodejs":10}, {"java":15}, "python":11]}; var doc2 = {"name": "Bryan", "webinars": 30}; var coll = db.collection("content") coll.insertMany( [doc1, doc2], function(err, result){ assert.equal(err, null); assert.equal(2, result.insertedCount); console.log("Sweet, inserted "+ result.insertedCount); cb(result); }); }
http://docs.mongodb.org/manual/data-modeling/
![Page 52: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/52.jpg)
WriteConcerns
![Page 53: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/53.jpg)
WriteConcern w:1
![Page 54: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/54.jpg)
WriteConcern w:2
![Page 55: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/55.jpg)
WriteConcern j:true
![Page 56: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/56.jpg)
Different WriteConcerns
var insertSuperImportant = function(db, cb){ var customer = {"name": "Manny Delgado", "age": 14}; var coll = db.collection("customers"); var writeConcern = {"w": "majority"}; col.insertOne( customer, writeConcern, function(err, result){ assert.equal(err, null); assert.equal(1, result.insertedCount); console.log("Inserted super important record"); cb(result); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/WriteConcernError.html
![Page 57: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/57.jpg)
Different WriteConcerns
var insertSuperImportant = function(db, cb){ var customer = {"name": "Manny Delgado", "age": 14}; var coll = db.collection("customers"); var writeConcern = {"w": "majority"}; col.insertOne( customer, writeConcern, function(err, result){ assert.equal(err, null); assert.equal(1, result.insertedCount); console.log("Inserted super important record"); cb(result); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/WriteConcernError.html
![Page 58: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/58.jpg)
Read Preference
![Page 59: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/59.jpg)
59
Read Preference
• Read from Primary (default) ReadPreference.PRIMARY • Read from Primary Preferably ReadPreference.PRIMARY_PREFERRED • Read from Secondary ReadPreference.SECONDARY • Read from Secondary Preferably ReadPreference.SECONDARY_PREFERRED • Read from Nearest Node ReadPreference.NEAREST
http://mongodb.github.io/node-mongodb-native/2.0/api/ReadPreference.html
![Page 60: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/60.jpg)
Read From Nearest
var readNearestWaterMelonColor = function(db, cb){ var rp = ReadPreference.NEAREST; var coll = db.collection("products", {ReadPreference:rp}); var query = {"color": "water melon green"}; collection.find(query).toArray(function(err, docs){ assert.equal(err, null); assert.equal(1, docs.length); console.log("So many products: "+ docs.length); console.dir(docs); cb(docs); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/ReadPreference.html
![Page 61: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/61.jpg)
Read From Nearest
var readNearestWaterMelonColor = function(db, cb){ var rp = ReadPreference.NEAREST; var coll = db.collection("products", {ReadPreference:rp}); var query = {"color": "water melon green"}; collection.find(query).toArray(function(err, docs){ assert.equal(err, null); assert.equal(1, docs.length); console.log("So many products: "+ docs.length); console.dir(docs); cb(docs); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/ReadPreference.html
![Page 62: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/62.jpg)
Read From Nearest
var readNearestWaterMelonColor = function(db, cb){ var rp = ReadPreference.NEAREST; var coll = db.collection("products", {readPreference:rp}); var query = {"color": "water melon green"}; collection.find(query).toArray(function(err, docs){ assert.equal(err, null); assert.equal(1, docs.length); console.log("So many products: "+ docs.length); console.dir(docs); cb(docs); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/ReadPreference.html
![Page 63: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/63.jpg)
Aggregation
![Page 64: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/64.jpg)
Aggregation
var aggregateAvgAgeGender = function( db, cb){ //{age:XX, name:"user name", gender: "M/F"} var pipeline = [ {$group: { "_id": "$gender", avg_age: {$avg: "$age"}}}, ]; var coll = db.collection("users"); var cursor = coll.aggregate(pipeline); cursor.forEach( function(x){ console.log("Gender " + x._id + " age average " + x.avg_age) }, function(x) { cb(cursor); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#aggregate
![Page 65: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/65.jpg)
Aggregation
var aggregateAvgAgeGender = function( db, cb){ //{age:XX, name:"user name", gender: "M/F"} var pipeline = [ {$group: { "_id": "$gender", avg_age: {$avg: "$age"}}}, ]; var coll = db.collection("users"); var cursor = coll.aggregate(pipeline); cursor.forEach( function(x){ console.log("Gender " + x._id + " age average " + x.avg_age) }, function(x) { cb(cursor); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#aggregate
![Page 66: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/66.jpg)
Aggregation
var aggregateAvgAgeGender = function( db, cb){ //{age:XX, name:"user name", gender: "M/F"} var pipeline = [ {$group: { "_id": "$gender", avg_age: {$avg: "$age"}}}, ]; var coll = db.collection("users"); var cursor = coll.aggregate(pipeline); cursor.forEach( function(x){ console.log("Gender " + x._id + " age average " + x.avg_age) }, function(x) { cb(cursor); }); }
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#aggregate
![Page 67: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/67.jpg)
Aggregation
var aggregateAvgAgeGender = function( db, cb){ //{age:XX, name:"user name", gender: "M/F"} var pipeline = [ {$match:{"age": $gt: 18}}, {$group: { "_id": "$gender", avg_age: {$avg: "$age"}}},
{$project:{"ID": "$_id", "average": "$avg_age" }} ]; var cursor = coll.aggregate(pipeline); var coll = db.collection("users"); cursor.forEach( function(x){ console.log("Gender " + x._id + " age average " + x.avg_age) }, function(x) { cb(cursor); });}
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#aggregate
![Page 68: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/68.jpg)
ODM's
![Page 69: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/69.jpg)
Mongoose
![Page 70: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/70.jpg)
70
Mongoose
• Schema Validation
• Casting
• Business Logic Wrapper
• http://mongoosejs.com/
![Page 71: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/71.jpg)
Simple Mongoose
var mongoose = require('mongoose'), assert = require('assert') var Schema = mongoose.Schema; //define a schema var userSchema = new Schema({ name: String, age: Number}) //create static members userSchema.statics.findByName = function( name, cb){ return this.find( {"name": name}, cb); } …
![Page 72: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/72.jpg)
Simple Mongoose
… //generate a model var User = mongoose.model('User', userSchema); //initiate the new user, validates the given arguments var u1 = User({name:"Many Delgado", age:14}); //just save it u1.save(function(err){ assert.equal(null, err); });
![Page 73: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/73.jpg)
73
Other Projects
Project Repository
MongoSkin https://github.com/kissjs/node-mongoskin
Mongolia https://github.com/masylum/mongolia
Mongojs https://github.com/mafintosh/mongojs
MongoSmash https://github.com/bengl/mongosmash
![Page 74: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/74.jpg)
MEAN Stack
![Page 75: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/75.jpg)
75
MEAN Stack
• MongoDB
• Express.js
• Angular JS
• Node.js
![Page 76: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/76.jpg)
Express is a minimal and flexible Node.js web application framework that provides a robust
set of features for web and mobile applications.
![Page 77: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/77.jpg)
AngularJS lets you extend HTML vocabulary for your application. The resulting
environment is extraordinarily expressive, readable, and quick to develop
![Page 78: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/78.jpg)
Building your first app with MongoDB: Creating a REST API using the MEAN Stack
https://www.mongodb.com/blog/post/building-your-first-application-mongodb-creating-rest-
api-using-mean-stack-part-1
![Page 79: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/79.jpg)
Meteor
![Page 80: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/80.jpg)
Meteor is a complete open source platform for building web and mobile apps in pure
JavaScript.
![Page 81: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/81.jpg)
81
Meteor
• Responsiveness
• Reactiveness
• Multiplatform
• Unified Package System
• Hot Deploys
https://www.meteor.com/try
![Page 82: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/82.jpg)
METEOR: Build IOS and Android Apps that are a delight to use
http://www.mongodb.com/blog/post/meteor-build-ios-and-android-apps-are-delight-use
![Page 83: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/83.jpg)
Recap
![Page 84: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/84.jpg)
84
What we talked about today…
• Node.js is a very productive language – Our driver is highly adopted – Updated – Fully compatible
• CRUD Operations – Insert, Update, Remove, Delete
• Write Concerns – Flexible to write
• Read Preferences – Flexible to read
• Aggregation Framework – Analytics at your fingertips
![Page 85: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/85.jpg)
85
Large Ecosystem
• Mongoose • Mean Stack • Meteor • Many other projects
![Page 86: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/86.jpg)
86
Where to next?
• Questions on the driver: – https://groups.google.com/forum/#!forum/node-mongodb-native
• Issues: – https://jira.mongodb.org/browse/NODE/?
selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel
• Tutorial: – http://mongodb.github.io/node-mongodb-native/2.0/
• Todays code: – https://github.com/nleite/firstappnodejs
• Other: – http://www.mongodb.com/norberto
![Page 87: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/87.jpg)
87
For More Information
Resource Location
Case Studies mongodb.com/customers
Presentations mongodb.com/presentations
Free Online Training education.mongodb.com
Webinars and Events mongodb.com/events
Documentation docs.mongodb.org
MongoDB Downloads mongodb.com/download
Additional Info [email protected]
Blog blog.mongodb.com
![Page 88: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/88.jpg)
88
Register now: mongodbworld.com!!
Use Code NorbertoLeite for additional 25% Off!*Come as a group of 3 or more – Save another 25%!
![Page 89: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/89.jpg)
http://cl.jroo.me/z3/v/D/C/e/a.baa-Too-many-bicycles-on-the-van.jpg
Questions?
@nleite [email protected] http://www.mongodb.com/norberto
![Page 90: MongoDB and Node.js](https://reader033.fdocuments.net/reader033/viewer/2022042817/55a9b4ec1a28abc7698b4757/html5/thumbnails/90.jpg)