Sunday, June 11, 2017

MongoDB & Node.js

MongoDB & Node.js


⇰ Creating a Database

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/<dbname>";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});


⇰ Creating a Table

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/<dbname>";

MongoClient.connect(url, function(err, db) {
 if (err) throw err;
 db.createCollection("<tablename>"function(err, res) {
  if (err) throw err;
 console.log("Table created!");
 db.close();
  });
});

⇰ Insert Into Table

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/<dbname>";

MongoClient.connect(url, function(err, db) {
 if (err) throw err;
 var myobj = [
  { name: 'Dilani', address: 'Karainagar'},
  { name: 'Inthu', address: 'Jaffna'}
  ];

  db.collection("<tablename>").insert(myobj, function(err, res) {
  if (err) throw err;
  console.log("Number of records inserted: " + res.insertedCount);
  db.close();
  });
});

⇰ Select One

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/<dbname>";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  db.collection("<tablename>").find({}).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

⇰ Filter the Result

var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/<dbname>";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var query = { address: "Karanagar" };  db.collection("<tablename>").find(query).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

⇰ Sort the Result

var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/<dbname>";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var mysort = { name: 1 };  db.collection("<tablename>").find().sort(mysort).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});


⇰ Delete Record

var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/<dbname>";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var myquery = { address: 'Mountain 21' };  db.collection("<tablename>").remove(myquery, function(err, obj) {
    if (err) throw err;
    console.log(obj.result.n + " document(s) deleted");
    db.close();
  });
});


⇰ Drop Collection

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  db.collection("customers").drop(function(err, delOK) {
    if (err) throw err;
    if (delOK) console.log("Table deleted");
    db.close();
  });
});

⇰ Update Document

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/mydb";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var myquery = { address: "Karanagar" };
  var newvalues = { name: "Inthu", address: "Karanagar" };
  db.collection("customers").update(myquery, newvalues, function(err, res) {
    if (err) throw err;
    console.log(res.result.nModified + " record updated");
    db.close();
  });
});

No comments:

Post a Comment