credit: https://medium.com/@joelrodrigues/how-to-access-mongodb-from-node-js-e37c587f226a What is MongoDB? is a document holding data in JSON format. Since it’s not based on joining, it’s easy to distribute data across servers by it’s sharding feature. MongoDB database Developers can develop application which can be agnostic about deployments. MongoDB supports scaling out as opposed to RDBMS which supports scaling up. Mongo shell is written using C++ V8 for administering the MongoDB. credit: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/ What is node.js _node.js_ is basically a _C++_ program that you control using _V8 javascript_ . So any applications you write using _node.js_ will be written in _javascript_ and it will control this _C++_ application ( _node.js_ ) and you’ll be able to say something like they made request for this resource and your application actually in _javascript_ will say: okey, they made a request for this resource, I don’t have to respond to that. Now respond accordingly. connects to using drivers. Installing bit release shall be avoided because it limits the addressable size of data using the default storage engine. Node.js MongoDB 32 What is JSON? To a layman, is merely a string, anyways. objects are composed of pairs. Keys must be strings, Keys and values must be separated by semi-colons. Fields within a object are separated by commas as delimiters. objects are opened and closed using curly braces: [JSON](http://json.org/) JSON key-value JSON JSON {"string" : "sting value goes here","date" : "2015-10-27T22:35:21.908Z","number" : 123,"object" : {"key1" : "value1","key2" : "value2"},"boolean" : true,"array" : ["test",{ "key1" : "a", "key2" : "b" },[ "abc", "xyz" ]]} The above example shows that supports a number of data-types. The field shows an example of nested values. As we can see and - data models commonly make use of nesting and even what we might call . The flexibility provides makes it simple to implement different data access patterns by creating objects that contain all the data required to render a webpage full of content or say to provide another type of data view for users with very few requests. JSON object object array MongoDB deep nesting JSON What is BSON? stores data as (binary ). Here is the comparison: MongoDB [BSON](http://bsonspec.org/) JSON // JSON{"hello": "world"} // BSON"\x16\x00\x00\x00\x02hello\x00 \x06\ x00\ x00\ x00world\ x00\ x00 " drivers send and recieve data in format and when the data is written to , it’s stored in . MongoDB BSON MongoDB BSON On the application side, the drivers map to most appropriate native data types. It’s BSON lightweight traversable, to support a variety of operations necessary for writing, reading and indexing documents MongoDB efficient, meaning encoding/decoding data to/from BSON as the drivers need to do can be performed very quickly. doesn’t distinguishes between s and s. Doesn’t supports s. extends the JSON value types to include s, s, s and binary data to support images and a number of other types of data. JSON integer float date BSON integer double date Installation for Windows To access the and directly from shell, Change the path for MongoDB: mongo mongod Go to System Properties Go to Advanced settings Click Environment variables Go to System variables Go to (this is where Windows looks for executables) Path Add the ’s startup location (eg: ) MongoDB C:\Program Files\MongoDB\Server\3.2\bin Sometimes, you might need to refresh the system to see these shortcuts working However before using from terminal, create the directory for to data by following the below commands: mongodb C:\data\db MongoDB store md \data\db Notice, the before - this makes sure that the directory is created in root directory (in this case ) only. \ data\db C:\ Now, if you type and hit Enter, it starts running. If you read the log printed on the terminal, it says that is listening on and that i.e. the default path - which we created lately. mongod MongoDB port=27017 dbpath=C:\data\db Now, once is running, we can start which makes a connection to port . mongod mongo 27017 Doing some CRUD For the sake of testing this installation, we’ll do a simple document using: insert and (or in relational terms) using the below command: db.names.insert({name:'testing'}) find select db.names.find() Where is an example of document. Also, to make the result of much readable, we can chain command. {name:'testing'} .find() .pretty() We can run as a as well. MongoDB service In , documents are stored in colletions which are organized into databases. To see databases present in run MongoDB MongoDB show dbs To insert a document into a collection, we first need to know how to specify that collection in the command. A collection and database that contains it form a . When doing operations, we reference the global variable . This variable holds the reference to the database we’re currently using. To switch to a database names type . namespace CRUD db test MySQL like command use test The insert operation returns a document as well, where set to true indicates that the record (or better say, document) was inserted successfully. All documents must have an _id field. Each document in a collection must have a unique document id. At the heart of the query language for is a query by example strategy. We can pass a blank object , key value pair . The result of command is not a mere array of documents, it’s instead a . We can see this: acknowledged underscore MongoDB {} {name: "Uber"} find() cursor object var c = db.names.find();c.hasNext() //returns true, meaning there is a document yet to be visited by this cursorc.next() //grabs that document Building using node To build applications using , we first need to . node.js install it Let’s create a simple server: http // Source: howtonode.org/hello-node // Load the http module to create an http server.var http = require('http'); // Configure our HTTP server to respond with Hello World to all requests.var server = http.createServer(function (request, response) {response.writeHead(200, {"Content-Type": "text/plain"});response.end("Hello World\n");}); // Listen on port 8000, IP defaults to 127.0.0.1server.listen(8000); // Put a friendly message on the terminalconsole.log("Server running at "); http://127.0.0.1:8000/ Store the above code in a file, let’s say .js app.js And how do we start it? In the terminal, go to the location of the file and type . To verify, if it’s running or not, go to . Notice port , which ‘ve pecified in the above code. node app.js http://localhost:8000 8000 we What is NPM var express = require('express'), cons = require('consolidate'), mongodb = require('mongodb'); is how we add an external library in a application. If we run this code through terminal, we’ll get the below error: require() node.js node To get this missing thing as a package, we can use command line . In real projects, there will be a bunch of package dependencies. Installing them all one-by-one will be a big pain. To resolve this comes file. This file contains meta data about the dependencies: express node node npm install express package.json {"name": "project name goes here","version": "0.1.2","description": "npm introduction","main": "app.js","dependencies": {"consolidate": "~0.13.1","express": "~4.13.3","mongodb": "~2.1.3"},"author": "ABC","license": "MIT"} With this file in , simply running will get all the packages for us inside directory. There’s also a way to install packages globally. place npm install project’s local node_modules What is Node.js Driver The driver communicates with the server using it’s wire protocol. It handles things like opening sockets, detecting errors and managing connections to replica sets. To include the driver in an application, use code and install using . As we can see, it’s a node package. Try to connect to using this package: MongoDB var mongodb = require('mongodb'); npm install mongodb just MongoDB var MongoClient = require('mongodb').MongoClient,assert = require('assert'); var url = 'mongodb://localhost:27017/startup'; MongoClient.connect(url, function(err, db) { assert.equal(null, err);console.log("Successfully connected to server"); // Find some documents in our collectiondb.collection('startup').find({}).toArray(function(err, docs) { // Print the documents returneddocs.forEach(function(doc) {console.log(doc.name);}); // Close the DBdb.close();}); // Declare successconsole.log("Last call");}); In order to use the , it’s important to have a solid understanding of the nature of in , including database requests. MongoDB Node.js driver asynchronous IO javascript The _mongo_ shell is synchronous meaning that when you issue a find command, it blocks waiting for command to return before continuing. This is not the case with _Node.js_ driver. Whether you’re doing a query, as we are here in the call defined or just setting up a connection to the database. As is common in _Javascript_ applications, the _Node.js_ driver we’re going to use is designed so that it’s methods function asynchronously. What this means is that instead of waiting on a return value from any methods we call, we instead pass in a callback function. The second parameter in the above call is a callback function. And is going to handle the result of this connection operation. Because of nature of , the following message gets printed before any database values: MongoClient.connect anysnchronous node.js _Last call_ What is Express is a module that handles routing, request parameters and other details of requests. Express node.js HTTP Here is a basic example of setting up a basic server: Express var express = require('express'),app = express(); app.get('/', function(req, res){res.send('Hello World');}); app.use(function(req, res){res.sendStatus(404);}); var server = app.listen(3000, function() {var port = server.address().port;console.log('Express server listening on port %s', port);}); Implementing HTML Templates The following example shows how to a template: implement HTML //file: app.jsvar express = require('express'), //set up the Express serverapp = express(), //use it to create an Express appengines = require('consolidate'); //use the wrapper library app.engine('html', engines.nunjucks); //register nunjucks template engine app.set('view engine', 'html'); //as being associated with the html extension, set the view engine app setting app.set('views', __dirname + '/views'); //specify template location app.get('/', function(req, res) {res.render('hello', { name : 'Templates' });}); app.use(function(req, res){res.sendStatus(404);}); var server = app.listen(3000, function() {var port = server.address().port;console.log('Express server listening on port %s', port);}); And the html //file: views/hello.html <h1>Hello, {{name}}!</h1> In the above code, is basically a set of wrappers for a number of template engines for . requires certain libraries to have a certain interface and handles that for us. is a variable which allows us to access to the directory in which the application file (in our case, ) resides. Since, we’ve passed - we’ll see the below output: consolidate Express Express consolidate __dirname node.js app.js name : 'Templates' Here’s a more advanced version which connects to the database as well: //file: app.js var express = require('express'),app = express(),engines = require('consolidate'),MongoClient = require('mongodb').MongoClient,assert = require('assert'); app.engine('html', engines.nunjucks);app.set('view engine', 'html');app.set('views', __dirname + '/views'); MongoClient.connect('mongodb://localhost:27017/startup', function(err, db) { assert.equal(null, err);console.log("Successfully connected to MongoDB."); app.get('/', function(req, res){ db.collection('startup').find({}).toArray(function(err, docs) {res.render('startup', { 'name': docs } );}); }); app.use(function(req, res){res.sendStatus(404);}); var server = app.listen(3000, function() { var port = server.address().port; console.log('Express server listening on port %s.', port); }); }); and the html //file: views/startup.html <h1>Hello, {{name}}!</h1> Handling GET requests using Express var express = require('express'),app = express(),engines = require('consolidate'); app.engine('html', engines.nunjucks);app.set('view engine', 'html');app.set('views', __dirname + '/views'); // Handler for internal server errorsfunction errorHandler(err, req, res, next) {console.error(err.message);console.error(err.stack);res.status(500).render('error_template', { error: err });} app.get('/:name', function(req, res, next) {var name = req.params.name;var getvar1 = req.query.getvar1;var getvar2 = req.query.getvar2;res.render('hello', { name : name, getvar1 : getvar1, getvar2 : getvar2 });}); app.use(errorHandler); var server = app.listen(3000, function() {var port = server.address().port;console.log('Express server listening on port %s.', port);}); The template is: hello <h1>Hello, {{name}}, here are your GET variables:</h1><ul><li>{{getvar1}}</li><li>{{getvar2}}</li></ul> and the template goes here: error <h1>Error: {{error}}</h1> In the above app, we’re just registering only one route i.e. . The colon says to take this part of the URL and store in a variable called . /:name name Will share further notes next week. Photos Originally published at xameeramir.github.io .