Skip to main content

Node.js MongoDB

Now a days all web based application has some database to store the data at the back end.
Nodejs frame work have the ability to work with both relational and non relational database.

Node.js MongoDB 

NoSQL database mongodb quite fame because of its ability to store the any sort of data at any format.In order work we have to download required module.Mongodb required the module to be installed is mongoose.And also with these modules we can perform many operations.Before that we have to get start mongodb in nodejs.
1.Install MongoDB Driver
* We can download from official mongodb driver or open in terminal and execute it
    npm install mongodb

* Node.js used this module to manipulate mongodb database.
  var mongo = require('mongodb');

2.Node.js MongoDB Create Database

To create a database:
1.Create the Mongoclient object
2.Specify the url connection (correct ip address and name of the database want to created)
  i)The first is 'mongodb' which specifies that we are connecting to a mongoDB database.
 ii)The next is 'localhost' which means we are connecting to a database on the local   machine.
 iii)The next is 'EmployeeDB' which is the name of the database defined in our MongoDB database.
3.Connect to our database 
4.Connection established





5.Closing the connection.
To run the database: node EmployeeDB.js
Important:In MongoDB, a database is not created until it gets content!
3.Node.js MongoDB Create Collection
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  //Create a collection name "customers":
  dbo.createCollection("customers", function(err, res) {
    if (err) throw err;
    console.log("Collection created!");
    db.close();
  });
});
Important: In MongoDB, a collection is not created until it gets content!

4.Node.js MongoDB Insert













By adding console we can check whether that data was inserted or not.
or
To check that the data has been properly inserted in the database, you need to execute the following commands in MongoDB
1.Use EmployeeDB
2.db.Employee.find({Employeeid :4 })
5.Node.js MongoDB Query










Output:









6.Node.js MongoDB Insert












7.Node.js MongoDB Update






8.Delete Document















reference: www.guru99.com
           www.w3schools.com


Comments

Popular posts from this blog

ER Diagram

ER is a high level conceptual data base diagram.ER modeling helps us to create well designed data base with systematic analyzement. Thats why it is very important to practice er diagram before create a database. Actually ER Diagram explains the logical structure of database.Even though ER diagram is similar to the flowchart it become unique because of its symbols and conceptual based exposes.

GIT HUB

Git hub is a web based hosting service for version control using git.Git hub is a project based management system.It is a good platform for the developers.Git hub is a SCM(Source Code Management. Now we can go through the git hub installing and coding. 1) Installing a git hub in a Linux (in Linux all coding are followed in terminal)         sudo apt-get install git 2)Configuring git hub    When we completed our installing after that we should have to configure our account as follows          get config -- global user.name "user name"           get config -- global user.email "email id"   once we configured our account no need to configure each time. Mainly seven steps are available to push the files in to git hub 1)git<>init 2)git<>add<>./* 3)git<>status 4)git<>commit<>-m<>detail 5)git<>remote<>add<>origin...