Node.js is one of the fastest growing and most popular application frameworks today. It offers a set of features that is both useful and challenging to modern web and back-end developer. Most engineers think of it as a cool, simple, and fast application framework that makes iterative development easier.
In this webinar we will talk about how Node.js and MongoDB interact, discuss the best ways to get started, and walk through how to use MongoDB and Node.js in a set of use cases.
9. 9
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
23. boot up MongoDB Server
$ mkdir ~/firstappdb
$ mongod --dbpath ~/firstappdb
24. boot up MongoDB Server
$ mkdir ~/firstappdb
$ mongod --dbpath ~/firstappdb --auth
--keyfile ~/n.pem
https://www.mongodb.com/products/mongodb-enterprise-advanced
25. boot up MongoDB Server
$ mkdir ~/firstappdb
$ mongod --dbpath ~/firstappdb --auth
--keyfile ~/n.pem
https://www.mongodb.com/products/mongodb-enterprise-advanced
26. var MongoClient = require('mongodb').MongoClient,
assert = require('assert');
Connect
27. var MongoClient = require('mongodb').MongoClient,
assert = require('assert');
//connection uri
var uri = "mongodb://localhost:27017/firstapp"
Connect
28. 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
29. 29
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
72. 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);
}
…
73. 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);
});
77. Express is a minimal and flexible Node.js web
application framework that provides a robust
set of features for web and mobile
applications.
78. AngularJS lets you extend HTML vocabulary
for your application. The resulting
environment is extraordinarily expressive,
readable, and quick to develop
79. Building your first app with MongoDB: Creating
a RESTAPI using the MEAN Stack
https://www.mongodb.com/blog/post/building-
your-first-application-mongodb-creating-rest-
api-using-mean-stack-part-1
85. 85
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
89. 89
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 info@mongodb.com
Blog blog.mongodb.com
Basic Driver installation for Node.js is to follow npm repository
We should create a project folder
Initialized it with NPM
Add the dependency to MongoDB lattes version the file should resemble something very similar to the shown example
We should create a project folder
Initialized it with NPM
Add the dependency to MongoDB lattes version the file should resemble something very similar to the shown example
- But do not forget to edit the dependencies accordingly!
Raises a server listening on port 27017 by default and pointing to ~/firstappdb
Do not forget that mongodb is ready for the authentication + authorization + auditing
Do not forget that mongodb is ready for the authentication + authorization + auditing
Pooling: there are no traditional concept of connection pools since this is a single threaded process. We use sockets to pupeline the commands to MongoDB.
Failover consists on buffering up the commands till the servers are available
These can be controlled by bufferMaxEntries, numberOfRetries and retryMiliSeconds
- Define the query to match the wanted documents
$set operator that will perform the change
In this case will produce the set of new array field
Verify that the operation occurred without errors and matches the expected end result
In this case 1 updated document
- Performs a multikey query operation
- The only variation on this case is that we are not going to handle the results object but the cursor returned by the query method
Change this slid
- Also mention the fact that we can specify the way we want to read from using replica tags
- Define the Nearest read preference
Set it at collection level
We can set this at connection, db, collection and instruction level
don't forget this is a pipeline of operations
We can build it in a programmatically shape to adjust to what we need to extract
Mongojs – simulates the mongoshell from nodejs
Mongoskin – layer for mongo-native nodejs driver
Mongolia – lightweight ODM