Node University - Node.js and MongoDB

Course by Azat Mardan

Notes

Why Server-Side JavaScript?

Node was originally born out of this problem --- how can you handle two things at the same time

  • Ryan Dahl, The Creator of Node.js

  • Non-blocking I/O: performant

  • Fast: browser arms race (V8)

  • One language across the stack

  • Expressive: don't waste time on setup

  • Solid standard (ECMA)

Node is Single-Threaded

Node.js is single-threaded by design to make asynchronous processing simpler. Multi-threading can be very complex: racing condition, deadlocks, priority inversions...

__What is Node under the hood - Node Core: V8, libev, libeio

  • Libev: The event loop
  • LibEio: Async I/O
  • LibUv: Abstraction on libEio, libev, c-ares (for DNS) & iocp (for Windows)

"Everything is a callback... in Node everything is non-blocking and so it doesn't allow you to just sit there and then return the response."

--Ryan Dahl

Node.js Global Object

  1. Node JS at its core is JavaScript
  2. global.require() === require()
  3. global.process === process
  4. global.console === console

Node.js REPL (Shell/Console)

Three ways to run node:

  1. node
  2. node -e "console.log('hi')"
  3. node file.js

Node.js Modules

Node Require Example:

var express = require('express');
var app = express();

Modules can live in different places with JavaScript. They can be on local machines, virtual machines, servers, remote URI locations, or anywhere.

In a module, we need to export either a function or an object

  • module.exports = function(ops) {...}
  • module.exports = {...}
  • exports.methodA = functions(ops) {...}
  • exports.obj = {...}

Node.js, Web Apps and http Core Module

node-dev or nodemon - will auto restart application upon change in file

Create, Delete, Read, and Update with Mongo

C.A.P. theorem :
- C onsistency (strong vs. eventual-delay)
- A vailability
- P artition tolerance (on cluster)

SQL; C+A

No SQL!
- A+P from C.A.P.
- No relationships in the database
- Redundancy is good

Types of NoSQL Databases:
- Key-value stores (Redis, think hash tables)
- Document stores (mongoDB, think JSON)
- Columnar stores (hbase, average age)
- Graphs stores (neo4j)

MongoDB:
- document store NoSQL database
- great at distributed and scaling

Launching MongoDB:
- $ mongod
- see info in terminal; default port is 27017 (for the database to accept common connections from the client)

MongoDB Shell(mongo):
- $ mongo
- ran from a separate terminal than the server which is left running
- To test the database, use the JavaScript-like interface and commands save and find:
- > db.test.save({a:1})
- > db.test.find()
- Useful MongoDB Shell commands:
- > help - show other commands
- > show dbs - list other databases
- > use board - use database
- > show collections -
- > db.messages.remove();
- > var a=db.messages.findOne();
- > print json(a);
- > a.message="hi";
- > db.messages.dave(a);
- > db.messages.find({});
- > db.messages.update({name:"John"},{$set: {message:"bye"}});
- > db.messages.find({name:"John"});
- > db.messages.remove({name:"John"});

MongoDB native driver vs. MongoDB Shell

Node.js Native Driver for MongoDB https://github.com/christkv/node-mongodb-native

$ npm install mongodb --save

Establishing Connection

var MongoClient = require('mongodb').MongoClient
    , assert = require('assert');
    
// Connection URL
var url = 'mongodb://localhost:27017/myproject':
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    console.log("Connected correctly to server");
    
    db.close();
});

Creating insertDocuments
creating_insertDocuments

Applying insertDocuments
applying_InsertDocuments

Updating Documents
updating_Documents

Finding Documents
Screenshot-from-2017-10-28-11-14-36

Native Driver Alternatives

Alternatively, for your own development, you could use other mappers, which are available as an extension of the Native Driver:

  • Mongoskin: the future layer for node-mongodb-native
  • Mongoose: asynchronous JavaScript driver with optional support for modeling
  • Mongolia: lightweight MongoDB ORM/driver wrapper
  • Monk: Monk is a tiny layer that provides simple yet substantial usabilities improvements for MongoDB usage within Node.js

MongoDB BSON Data Types

Binary JSON, or BSON, it is a special data type which MongoDB utilizes. It is like to JSON in notation, but has support for additional more sophisticated data types:

http://bsonspec.org

Node University - Node.js and MongoDB
Share this