Mongo DB
à Data – It is a raw
data.
à Database is used to
store the data (or) database is a well-organized format of data.
à DBMS – which are
used to organize the data in databases.
Mongo DB:
Mongo DB is a document oriented data module, which means in Mongo DB the
data is stored in the form of documents. These documents are nothing but BSON
object.
BSON object is like a JSON object but it contains different types (data types)
of values and it is stored in collections.
Front
End
|
Data
Base
|
In generally Mongo DB server contain databases. Databases have
collections and collections contain ‘n’ number of documents.
Mongo DB
Server
|
|
DBn | |
Difference between MYSQL and Mongo DB:
MYSQL
|
Mongo DB
|
It is schema oriented.
|
It is a semi/non schema
oriented.
|
To store data use Tables.
|
To store data in documents use
collections.
|
It is use SQL.
|
It is a non-SQL.
|
It is slower than Mongo DB.
|
It is faster than MySQL.
|
Usage of Mongo DB:
Step (1): - Install the Mongo DB
in local machines.
Step (2): - Use (or) open the
Mongo DB shell with below command.
(Mongo) à shell ready to use
By default it use
27017 port.
We can connect local db server with this url à mongodb://127.0.0.1:27017/
Step (3): - With the help of
below commands and methods we can perform different actions on data in database
server like crud operation.
show dbs;
With the help of this
command we can view databases which are presented database server.
use databasename;
Ex: use students;
With the help of this command we can create a
database.
db
With the help of “db”
command we can find current database.
Creating Collection:
db.createCollection('Users')
With the help of
create of collection () we can create the collection.
show collections;
To show the
collections in database we use above command.
2nd way of collection à db.collectname
db.users.insertOne({name: 'xyz',age:12})
Inserting record (or) Document objects:
With the help of 2 methods we can insert documents in
collection.
db.users.insertOne({name: 'xyz',age:12});
db.users.insertMany([{name: 'xy',age:13},{name: 'yz',age:14},{name: 'xab',age:15}]);
To Read Data:-
With the help of 2 methods we can read the data
find() àtotal records àdb.users.find();
findOne() àsingle record àdb.users.findOne();
Example:-
db.users.find({age:{$gt:20}},{name:1
});
db.users.find({age:{$gt:21}},{name:1
},limit(1));
To Update Data:-
With the help of 2 methods we can update the data
updateOne();
updateMany();
Example:-
db.users.updateOne({age:21},{$set:{age:25}});
db.users.updateMany({age:{$gt:11}},{$set:{age:25}});
To Delete Data:-
With the help of 2 methods we can delete the data
deleteOne();
deleteMany();
Example:-
db.users.deleteOne({age:{$lt:25}});
db.users.updateMany({age:{$lt:11}});
Above examples lt meansàlessthan, gt meansàgreaterthan;
Mongodb
|
Document àBSON object
|
insertOne()
insertMany()
|
One- ({}) àsingle obj,
Many- ([{},{},{}]) àmany objs
|
find()
findOne()
|
({}) àqueryfilter,
({},{}) àprojection
|
updateOne()
updateMany()
|
({}) à filter,
({condition},{action})
|
deleteOne();
deleteMany();
|
({}) àparameters
|
REST API interaction with Mongo DB server:
It is necessary/very important to store/get the
information from database to user. In generally user request http request to
handle this http request REST API provides different http handlers for
different http requests. It is necessary for handlers to interact with database
server to perform actions depending on the request type.
Steps to work with Mongo DB server with REST
API:
Step (1): We need to install the Mongo DB driver in Node JS
platform. With the help of below command we can install Mongo DB driver.
npm install mongodb --save
Step (2): Import the Mongo DB
driver like below.
const mongoClient=require('mongodb').MongoClient;
Here we are interacting with Mongo DB server with “mongoClient”
object.
Step (3): Connecting to Mongo
DB server.
mongoClient object provide a method call connect with
the help of this method we will connect to database server.
mongoClient.connect(mongodburl,(err,client)=>{
});
const mongodburl='mongodb://127.0.0.1:27017/';
Example:
const Express=
require('express');
var app=Express();
const
mongoClient=require ('mongodb').MongoClient;
const
mongodburl='mongodb://127.0.0.1:27017/';
var dbo;
var bodyParser =
require('body-parser')
mongoClient.connect(mongodburl,(err,client)=>{
if(err)
{
console.log('error in connection');
}
else
{
dbo=client.db('students');
console.log('successfully connect to
mongodb')
}
});
In the above example “dbo” is a database object
variable we are assigning database object to this variable with the help of
database method of client object.
dbo=client.db('students');
Get handler (get) interact with Mongo DB
object:
In order to read data from database get handler should
interact with database like below.
//Below we “/read” – any name.
app.get('/ read',(req,res)=>
{
dbo.collection('users').find({}).toArray((err,data)=>
{
if(err)
{
console.log(err);
res.send(err);
}
else
{
res.send(data);
}
})
})
Get handler (get) interact with Mongo DB
object:
//Below we “/ani” – any name.
app.get('/ani/:name', (req,res)=>
{
dbo.collection('student').find({name:req.params.name}).toArray((err,data)=>
{
if(err)
{
console.log(err);
res.send(err);
}
else
{
res.send(data);
}
})
});
Using nodemon:
Nodemon provides a beautiful solution for server
restarting to changes in a code. In generally we have to stop the server and
start the server again to add changes for application. It takes too much time
and cause inconvenience to developer.
By using nodemon we can overcome this problem.
Installing nodemon:
In command prompt install nodemon like below
npm install –g nodemon
With the help of this command we can install nodemon. In windows
operating system run this command as admin level.
In Linux systems use sudo command before above command.
Post handler interaction with database
object:
In generally post handler is used for post/create data
which is sent by client. The data is presenting request object. We can
access/extract the data with the help of below line
req.body
With the help of “body-Parser” module we can extract
data which is presented in “req.body” until we used body-Parser the req.body
hides the data or prints undefined.
With the help of below command we can install
body-Parser module.
npm install body-parser --save
We can import the below line
var bodyParser = require('body-parser')
app.use(bodyParser.json());
Note: Example is see assignment: 1
({name:req.params.name} à params is giving url.
{$set:{id:req.body.id}} à body is giving raw data.
Export and Importing of modules in express:
In express it is possible to create our own custom
modules and then export it after that we can import that exported module in
other module and gives the functionalities in exported module by this we can
implement the code reusability and share the functionalities/data between the
modules.
Example:
checkeven.js
var checkeven=(number)=>{
if(number%2==0)
{
return '${number} is even';
}
else
{
return '${number} is not even';
}
module.exports=checkeven; //exporting of a module
findeven.js
var even=require('./checkeven'); //importing of a module
let result=even(2);
console.log(result);
Routing in express:
In Express JS it is possible to create individual
routes by creating many applications for a single express app.
In order to create mini applications in single express
app, we have to use router interface in many applications like below.
Const/var Expre=require('express');
Const/var routh=Expre.Router();
Let’s take a synario an express app contains admin
routes and user routes.
admin routs(endpoints)
http:/localhost:8080/api/admin/**
user routs(endpoints)
http:/localhost:8080/api/user/**
In order to implement the above diagram we need to
following steps.
Step(1):- to create backend
service folder.
Step(2):- Install express
module.
Step(3):- create server.js
file
Step(4):- create routes
folder. In folder create admin.js, user.js files.
Step(5):- configure the
respect routes in respect files by creating mini files.
Like below example.
Example:
server.js
const Expre=require('express');
const routh=require('./routes/router');
const app=Expre();
const port=8080;
app.use('/api',routh);
app.listen(port,()=>{
console.log('server started at
${port}')
});
router.js
const Expre=require('express');
const routh=Expre.Router();
const admin=require('./admin');
const user=require('./user');
routh.use('/admin',admin);
routh.use('/user',user);
module.exports=routh;
admin.js
const Expre=require('express');
const routh=Expre.Router();
routh.get('/',(req,res)=>{
res.send('admin get');
});
routh.post('/',(req,res)=>{
res.send('admin post');
});
routh.put('/',(req,res)=>{
res.send('admin put');
});
routh.delete('/',(req,res)=>{
res.send('admin delete');
});
module.exports=routh;
user.js
const Expre=require('express');
const routh=Expre.Router();
routh.get('/',(req,res)=>{
res.send('user get');
});
routh.post('/',(req,res)=>{
res.send('user post');
});
routh.put('/',(req,res)=>{
res.send('user put');
});
routh.delete('/',(req,res)=>{
res.send('user delete');
});
module.exports=routh;
Exporting and importing of MongoDB database connection
object:
Webserver use different types of API’s to handle different types of
requests which are interact with database. So, that it is convenient to share
data base connection object among all API’s in an application for this we need
to create mongodb database configuration file at root level.
Exporting MongoDB database connection
object:
Create mongodb folder àcreate config.js file write below code.
config.js
const
mongoclient=require('mongodb').MongoClient;
const url='mongodb://127.0.0.1:27017/';
var dbo;
var init=()=>{
mongoclient.connect(url,(err,client)=>{
if(err)
{
console.log(err);
}
else{
dbo=client.db('Exam');
console.log('successfully connected
to database Exam');
}
})
};
var get=()=>{
return dbo;
}
module.exports={get,init}
Importing MongoDB database connection object
in API’s:
Users.js
const
Express=require('express');
const route=Express.Router();
const databaseobj=require('../Mongodb/dbconfig');
databaseobj.init();
route.get('/',(req,res)=>{
var dbo=databaseobj.get();
dbo.collection('userExam').find({}).toArray((err,data)=>
{
if(err)
{
console.log(err);
res.send(err);
}
else
{
res.send(data);
}
})
});
route.post('/',(req,res)=>{
var dbo=databaseobj.get();
dbo.collection('userExam').insertOne(req.body,(err,data)=>
{
if(err)
{
console.log(err);
}
else
{
res.send(data);
}
});
});
module.exports=route;
Integrating angular application with backend
services node JS:
So far we develop angular applications and express/node applications
individually that means there is no interaction between two applications/
servers. The time has taken to integrate these two servers/applications.
We need to do follow steps:-
Step (1): We have to install
cors (cross origin resource) module in express JS application/backend service.
Like below in express application
npm install cors --save
Step (2): After installing
cors we have to import it and use it.
Server.js
var cors=require('cors');
apps.use(cors());
Step (3): Register backend
server in environment folder.
environment.ts
production:false;
dataurl:’http://localhost:8080’;
Step (4): After that use that
environment object in dataservice.ts file
dataservice.ts
import { Injectable }
from '@angular/core';
import{environment} from 'src/environments/environment';
import{HttpClient} from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class NoticeDataServiceService {
constructor(private
http:HttpClient) { }
baseurl=environment.dataurl;
//Login
Postlog(data):Observable<any>{
let
url=`${this.baseurl}/api/Admin/login`;
return
this.http.post(url,data);
}
}
Password Hashing:-
It is mandatory to hide/encrypt the sensitive data
which is given by client. If the programmer didn’t hash the sensitive data that
will be stored in plain text format in databases. Then the backend/database
programmers can miss use the sensitive data. In order to overcome this we have
to encrypt sensitive data that is impossible to decrypt like hashing. Node JS
with the help of bcrypt module we can achieve this.
Installing and importing bcrypt module:-
With the help of
below command we can install bcrypt module in our project.
npm install bcryptjs
After installing using ‘require’ method we can import
bcrypt module like below.
const bcrypt=require('bcryptjs');
bcrypt module contain hash method with the help of
this hash method we can hash the sensitive data, plain text.
bcryptjs.hash(Plaintext, salt rounds, error callback);
Example:-
admin.js
route.post('/reg',(req,res)=>{
bcryptjs.hash(req.body.Password,10,(err,hp)=>{
req.body.Password=hp;
var dbo=databaseobj.get();
dbo.collection('Home').insertOne(req.body,(err,data)=>{
if(err){
res.send('err');
}else{
dbo.collection('Admin').find({UserName:req.body.UserName}).toArray((err,data)=>{
if(err){
console.log(err);
res.send(err);
}else{
res.send(data);
}
});
}
})
});
});
Comparing plain text/password with hashed
text/password:-
In order to compare plain text with hashed
password/text bcrypt module provides a method call compare with the help of
this method we can perform the comparison without decrypting/encrypting.
bcryptjs.compare(plaintext,hashedtext,error,callbackfunction);
Example:
admin.js
route.post('/login',(req,res)=>{
var dbo=databaseobj.get();
dbo.collection('Admin').find({ID:req.body.ID}).toArray((err,data)=>{
if(err)throw err;
if(data.length>0){
bcryptjs.compare(req.body.Password,data[0].Password,(err,result)=>{
if(err)throw err;
if(result){
res.send(data[0].Category);
}else{
res.send('Password not matched.')
}
});
}else{
res.send('User not
matched.')
}
});
})
Middle wares:
Middle wares are functions which will be execute
before the actual request handler. The middle ware function has access to
request object and response object. The middle ware function is look like.
var functionname = (req, res, next)=>{
}
The middle ware function contains 3 parameters.
(1) request object
(2) response object
(3) Next parameter.
The next parameter helps to execute next middle ware/
next request handler.
Usage of middle wares:
We can use middle ware in 2 ways
(1) We can specify to express application to execute
or call middle ware for every request.
Syntax: - expressobject.use(middlewarefunction);
(2) We can specify to express application to execute
middle ware function only for specific request handler not for all req.
Syntax: expressobject/routerobject.requesthandler('endpoint',
middlewarefunction,(req,res)=>
{
});
Example:
Middlewarefolder àsayHi.js
sayHi.js
var sayhi=(req,res,next)=>{
console.log(‘hi---’);
next();
});
module.exports=sayhi;
server.js
const Express=require('express');
const
bcryptjs=require('bcryptjs');
const sayhi=require('../Middleware/sayHi);
databaseobj.init();
const apps=Express();
const port=4030;
apps.use(bodyParser.json());
apps.use(sayhi);
apps.get('/',(req,res)=>{
res.send(get);
});
apps.post('/reg',(req,res)=>{
res.send(reg);
});
apps.listen(port,()=>{
console.log(`server started at ${port}`);
});
Authentication and Authorization:-
Authentication: It is the process of
proving something will be true, genuine and valid for application level
authentication required credentials (username, password).
Http protocols are stateless protocols means it didn’t
remember the request where it comes and http server treats every request as a
fresh request. So it is very difficult to user to send credentials for every
request to prove genuine user, with the help of below scenario we can overcome
this problem.
Data
Base
|
Middle
ware
|
Webclient
|
res
valid+jwt
As seen in the above diagram with the help of JSON web
token (Jwt) we can provide a beautiful solution for this problem.
JSON web token (jwt):- JSON web token is
used to share data between two (or) more servers or parties each jwt token
contain 3 parts.
(1) Header
(2) Payload
(3) Verified signature.
Steps to create JSON web token:-
Step (1): Installation of jwt
module.
With the help of below command we can install jwt
package/module in our project.
npm i jsonwebtoken
// i- install
Step (2): Importing JSON
module.
With the help of below syntax we can import jsonwebtoken.
const jwt=require('jsonwebtoken');
Step (3): Defining security
access token like below
const sat='security';
security – any name
we can give here.
Step (4): Generating signed jsonwebtoken.
With the help of signed method we can generate the
signed jsonwebtoken.
Syntax:
jwt.sign(payload, sat, expirestime);
Example: const jwtToken=jwt.sign({ID:data[0].ID},sat,{expiresIn:'50min'});
Use this sign method in login handler.
Example:
route.post('/login',(req,res)=>{
var dbo=databaseobj.get();
dbo.collection('Admin').find({ID:req.body.ID}).toArray((err,data)=>{
if(err)throw err;
if(data.length>0){
bcryptjs.compare(req.body.Password,data[0].Password,(err,result)=>{
if(err)throw err;
if(result){
const
jwtToken=jwt.sign({ID:data[0].ID},sat,{expiresIn:'50min'});
res.json({'msg':'success','token':jwtToken,'Category':data[0].Category});
}else{
res.send('Password not matched.')
}
});
}else{
res.send('User not
matched.')
}
});
})
At client side:
In login.ts
file subscribe method get the response.
If the response object contain message as success then
store the jwt token in localStorage at browser side with the help of below
method.
localStorage.setItem('token',data.token);
Example:
this.NoticeDataServ.Postlog(obj).subscribe(data=>{
console.log(data)
if(data.msg=='success'){
localStorage.setItem('token',data.token);
}
if(data.Category=='staff')
{
this.Route.navigate(['staff']);
}
else
if(data.Category=='student')
{
this.Route.navigate(['student']);
}
})
Authorization:
It is a process of deciding rights/premilizers of a
user about particular resources with the help of authorization it is possible
to protect some routes from unauthorized access.
Create folder à Middlewares à authorization.js
const jwt=require('jsonwebtoken');
const sat='security';
var checkAutho=(req,res,next)=>{
let
token=req.headers.authorization;
if(token==undefined){
res.json({'msg':'unauthorized access'});
}
else{
jwt.verify(token,sat,(err,data)=>{
if(err){
res.json({'msg':'token expired'});
}
else{
next();
}
})
}
}
module.exports=checkAutho;
Assignments
Assignment: 1-
const Express= require('express');
var app=Express();
const mongoClient=require ('mongodb').MongoClient;
const mongodburl='mongodb://127.0.0.1:27017/';
var dbo;
var bodyParser = require('body-parser')
mongoClient.connect(mongodburl,(err,client)=>{
if(err)
{
console.log('error in connection');
}
else
{
dbo=client.db('students');
console.log('successfully
connect to mongodb')
}
});
app.use(bodyParser.json());
app.get('/admin/read',(req,res)=>
{
dbo.collection('Admin').find({}).toArray((err,data)=>
{
if(err)
{
console.log(err);
res.send(err);
}
else
{
res.send(data);
}
})
})
app.get('/user/read',(req,res)=>
{
dbo.collection('Users').find({}).toArray((err,data)=>
{
if(err)
{
console.log(err);
res.send(err);
}
else
{
res.send(data);
}
})
})
app.post('/admin/postOne',(req,res)=>
{
dbo.collection('Admin').insertOne(req.body,(err,data)=>
{
if(err)
{
console.log(err);
}
else
{
dbo.collection('Admin').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.post('/admin/postMany',(req,res)=>
{
dbo.collection('Admin').insertMany(req.body,(err,data)=>
{
if(err)
{
console.log(err);
}
else
{
dbo.collection('Admin').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.post('/user/postO',(req,res)=>
{
dbo.collection('Users').insertOne(req.body,(err,data)=>
{
if(err)
{
console.log(err);
}
else
{
dbo.collection('Users').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.post('/user/postM',(req,res)=>
{
dbo.collection('Users').insertMany(req.body,(err,data)=>
{
if(err)
{
console.log(err);
}
else
{
dbo.collection('Users').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.put('/admin/updateO/:name',(req,res)=>
{
dbo.collection('Admin').updateOne({name:req.params.name},{$set:{id:req.body.id}},(err,data)=>
{
if(err)
{
res.send(err);
}
else
{
dbo.collection('Admin').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.put('/admin/updateM/:name',(req,res)=>
{
dbo.collection('Admin').updateMany({name:req.params.name},{$set:{id:req.body.id}},(err,data)=>
{
if(err)
{
res.send(err);
}
else
{
dbo.collection('Admin').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.put('/user/updateO/:name',(req,res)=>
{
dbo.collection('Users').updateOne({name:req.params.name},{$set:{id:req.body.id}},(err,data)=>
{
if(err)
{
res.send(err);
}
else
{
dbo.collection('Users').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.put('/user/updateM/:name',(req,res)=>
{
dbo.collection('Users').updateMany({name:req.params.name},{$set:{id:req.body.id}},(err,data)=>
{
if(err)
{
res.send(err);
}
else
{
dbo.collection('Users').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.delete('/admin/delO',(req,res)=>
{
dbo.collection('Admin').deleteOne({id:req.body.id},(err,data)=>
{
if(err)
{
res.send(err);
}
else
{
dbo.collection('Admin').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.delete('/admin/delM',(req,res)=>
{
dbo.collection('Admin').deleteMany({id:req.body.id},(err,data)=>
{
if(err)
{
res.send(err);
}
else
{
dbo.collection('Admin').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.delete('/user/delO',(req,res)=>
{
dbo.collection('Users').deleteOne({id:req.body.id},(err,data)=>
{
if(err)
{
res.send(err);
}
else
{
dbo.collection('Users').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.delete('/user/delM',(req,res)=>
{
dbo.collection('Users').deleteMany({id:req.body.id},(err,data)=>
{
if(err)
{
res.send(err);
}
else
{
dbo.collection('Users').find({}).toArray((err,data)=>{
if(err)throw err;
res.send(data)
});
}
});
})
app.listen('8181',()=>
{
console.log('server started at
8181 port')
});
No comments:
Post a Comment