Tuesday, June 6, 2017

19/05/2017

                                        Nodejs Mongo DB


Create a database

Create a database called "uki":
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/uki";

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



Create a table

Create a table called "students":
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/uki";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  db.createCollection("students", function(err, res) {
    if (err) throw err;
    console.log("Table created!");
    db.close();
  });
}); 
Insert in to table
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/student";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var insert = [
    { name:"mala",maths_marks:45,english_marks:53,science_marks:75},
      { name:"venu",maths_marks:80,english_marks:75,science_marks:85},
        { name:"Kala",maths_marks:32,english_marks:46,science_marks:53},
          { name:"Aruli",maths_marks:78,english_marks:85,science_marks:80},
            { name:"Sharu",maths_marks:80,english_marks:76,science_marks:65},
              { name:"Kumaran",maths_marks:32,english_marks:73,science_marks:84},
                { name:"Lucky",maths_marks:66,english_marks:90,science_marks:45},
                  { name:"Gva",maths_marks:71,english_marks:75,science_marks:56},
                    { name:"Raam",maths_marks:41,english_marks:65,science_marks:88},
  ];
    db.collection("studentmarks").insert(insert, function(err, res) {
      if (err) throw err;
      console.log("Number of records inserted: " + res.insertedCount);
      db.close();
    });
  });
 

No comments:

Post a Comment

HTML introduction

HTML stands for Hyper Text Markup Language. HTML describes the structure of web page using markup. HTML elements are the building blo...