CRUD – MongoDB using NodeJs in Local Machine

  • In order to access MongoDB database, we need to install MongoDB drivers. To install native mongodb drivers using NPM, open command prompt and write the following command to install MongoDB driver in your application.
npm install mongodb --save
  • For local Installation of MongoDB ( Refer Link ). Now, start the MongoDB server. (Assuming that your MongoDB database is at C:\MyNodeJSConsoleApp\MyMongoDB folder.)
mongod -dbpath C:\MyNodeJSConsoleApp\MyMongoDB
  • Create a node file and start coding. First Connecting MongoDB, local mongo alway opens connection in port 27017
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
  if(!err) {
     console.log("<------------We are connected------------>");
     //Write databse Insert/Update/Query code here..
  • For Create collections,
 db.createCollection("collectionName", function(err, collection) {
     console.log("<------------DB Created------------>");

  • For Insert Data,
var collection = db.collection(collectionName);

var doc1 = {'hello': 'doc1'};

var doc2 = {'hello': 'doc2'};
collection.insert(doc2, {w: 1}, function(err, result) { // Using the {w:1} option ensure you get the error back if the document fails to insert correctly.
    console.log("<------------Data Inserted------------>");

var lotsOfDocs = [{'hello': 'doc3'}, {'hello': 'doc4'}];
collection.insert(lotsOfDocs, {w:1}, function(err, result) {});

  • For Update data,
 db.collection(collectionName).update({hello:'doc2'},{$set:{hello:"docUpdated",firstName: 'James'}}, {w:1}, function(err, result) {

  • For Delete Data,
 db.collection(collectionName).remove({hello:'docUpdated'}, {w:1}, function(err, result) {

  • For View Data,
 db.collection(collectionName).find().toArray(function(err, items) {
     if(err) throw err;


Disable Right Click and Show Div

To do this we need 2 node, One is the div which the right click is to be disabled and the other is the div which going to show instead of “contextmenu”.

 var rClickDisabledDiv = document.getElementsByClassName("rightClickDisabled")[0];
 var showDivNode = document.getElementsByClassName("showDiv")[0];

The key here is how to disable the right click.

  rClickDisabledDiv.addEventListener("contextmenu",function(e){ = "block"; = e.pageY+"px"; = e.pageX+"px";

Rest will be piece of cake 🙂

function divOnRightClick(rClickDisabledDiv, showDivNode){
  rClickDisabledDiv.addEventListener("contextmenu",function(e){ = "block"; = e.pageY+"px"; = e.pageX+"px";
  document.body.addEventListener("click",function(){ = "none";


JavaScript Design Patterns

Creational Patterns   Abstract Factory Creates an instance of several families of classes   Builder Separates object construction from its representation   Factory Method Creates an instance of sev…

Source: JavaScript Design Patterns

JavaScript Cascade function

In a cascade, we can call many methods on the same object in sequence in a single statement. The trick is that the method itself should only return this. That way, each time you chain these methods…

Source: JavaScript Cascade function