MongoDB 

for 

javascripters

by Nacho Galieri
BeerJs Bogotá
by Nacho Galieri
BeerJs Bogotá
 What is MongoDB?
  • OpenSource

 

  • NoSQL Database

 

  • Document Database
by Nacho Galieri
BeerJs Bogotá
diff ./SQL ./NoSQL
1,7c1,7
< The data are stored in Tables And Rows.
< Have predefined schema.
< Are vertically scalable.
< Are scaled by increasing the horse-power of the hardware. 
< Uses Structured Query Language.
< Good for complex queries.
< Atomicity, Consistency, Isolation and Durability.
---
> The Data are stored in Document and Collections.
> Have dynamic schema for unstructured data.
> Are horizontally scalable. 
> Are scaled by increasing the databases servers in the pool of resources to reduce the load.
> Uses Unstructured Query Language.
> Bad for complex queries.
> Consistency, Availability and Partition tolerance.
by Nacho Galieri
BeerJs Bogotá
Mongo/Node - Package
$>  npm install mongodb --save
by Nacho Galieri
BeerJs Bogotá
Mongo/Node - Connection
var MongoClient = require('mongodb').MongoClient;

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {

   ..... 

  db.close();
});
by Nacho Galieri
BeerJs Bogotá
Mongo/Node - Insert Many
var insertDocuments = function(db, callback) {
  // Get the documents collection
  var collection = db.collection('documents');
  // Insert some documents
  collection.insertMany([
    {a : 1}, {a : 2}, {a : 3}
  ], function(err, result) {
    assert.equal(err, null);
    assert.equal(3, result.result.n);
    assert.equal(3, result.ops.length);
    console.log("Inserted 3 documents into the document collection");
    callback(result);
  });
}
by Nacho Galieri
BeerJs Bogotá
Mongo/Node - Update
var updateDocument = function(db, callback) {
  // Get the documents collection
  var collection = db.collection('documents');
  // Update document where a is 2, set b equal to 1
  collection.updateOne({ a : 2 }
    , { $set: { b : 1 } }, function(err, result) {
    assert.equal(err, null);
    assert.equal(1, result.result.n);
    console.log("Updated the document with the field a equal to 2");
    callback(result);
  });  
}
by Nacho Galieri
BeerJs Bogotá
Mongo/Node - Remove
var removeDocument = function(db, callback) {
  // Get the documents collection
  var collection = db.collection('documents');
  // Insert some documents
  collection.deleteOne({ a : 3 }, function(err, result) {
    assert.equal(err, null);
    assert.equal(1, result.result.n);
    console.log("Removed the document with the field a equal to 3");
    callback(result);
  });    
}
by Nacho Galieri
BeerJs Bogotá
Mongo/Node - Find
var findDocuments = function(db, callback) {
  // Get the documents collection
  var collection = db.collection('documents');
  // Find some documents
  collection.find({}).toArray(function(err, docs) {
    assert.equal(err, null);
    assert.equal(2, docs.length);
    console.log("Found the following records");
    console.dir(docs)
    callback(docs);
  });      
}
by Nacho Galieri
BeerJs Bogotá
Mongo/Node - All
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");

  insertDocuments(db, function() {
    updateDocument(db, function() {
      removeDocument(db, function() {
        findDocuments(db, function() {
          db.close();
        });
      });
    });
  });
});
by Nacho Galieri
BeerJs Bogotá

Hand On

Thanks for beers

About Me:

Ignacio 'Nacho' Galieri

web: http://elnachonerd.com

Twitter: @NachoNerd

GitHub: irgalieri

Questions?
References

MongoDB for JavaScripters

By Ignacio R. Galieri

MongoDB for JavaScripters

  • 485