of the node.js mongodb driver • find me on • web: http://christiankvalheim.com • twitter: christkv • github: christkv • email: [email protected] Who am I Tuesday, October 11, 2011
node.js + express + mongodb • Interesting parts of the driver What’s for dinner https://github.com/christkv/mongodb-presentation Code ON Tuesday, October 11, 2011
Connection = require('mongodb').Connection; var host = 'localhost'; var port = Connection.DEFAULT_PORT; var db = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:false}); var app = express.createServer(); app.get('/', function(req, res){ res.send('Hello World'); }); db.open(function(err, db) { if(err) throw err app.listen(8124); }); Tuesday, October 11, 2011
call to lastError required to check for the success of the operation • safe option ensures the second error call • you can also run the driver in strict mode Tuesday, October 11, 2011
gridStore.open(function(err, gs) { var stream = gs.stream(true); stream.on("data", function(chunk) { // Received a chunk of data }); stream.on("end", function() { // Finished streaming }); }); Tuesday, October 11, 2011
gridStore.open(function(err, gs) { var stream = gs.stream(true); stream.on("data", function(chunk) { // Received a chunk of data }); stream.on("end", function() { // Finished streaming }); }); PREFERRED Tuesday, October 11, 2011
(avoid overhead of parsing) • BSON JS parser will be reworked to work in browser • For analytics or other situations where you don’t do “post-processing” on server before the client Tuesday, October 11, 2011
Mongodb server • How to write a simple CRUD location app in node.js with Mongodb • Other cool parts of the driver • How to avoid the first pitfall of using nodeJS Tuesday, October 11, 2011