Node JS: Prof. Nalini N Scope VIT

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Node JS

Prof. Nalini N
SCOPE
VIT
Node JS

• Node.js is a server-side platform built on Google Chrome's JavaScript


Engine
• Node.js provides a rich library of various JavaScript modules which
simplifies the development of web applications.
• Node.js uses an event-driven, non-blocking I/O model that makes it
lightweight and efficient, perfect for data-intensive real-time
applications that run across distributed devices.
• eBay, General Electric, GoDaddy, Microsoft, PayPal, Uber, Wikipins,
Yahoo!, and Yammer to name a few
Features
• Asynchronous and Event Driven
• All APIs of Node.js library are asynchronous, that is, non-blocking.
• It essentially means a Node.js based server never waits for an API to return
data. The server moves to the next API after calling it and a notification
mechanism of Events of Node.js helps the server to get a response from the
previous API call.
• Very Fast
• Single Threaded but Highly Scalable
• Node.js uses a single threaded model with event looping.
• Node.js uses a single threaded program and the same program can provide
service to a much larger number of requests than traditional servers like
Apache HTTP Server.
• No Buffering − Node.js applications never buffer any data. These
applications simply output the data in chunks.
Node JS purpose

• Node.js can generate dynamic page content


• Node.js can create, open, read, write, delete, and close files on the
server
• Node.js can collect form data
• Node.js can add, delete, modify data in your database
Node Js----Hello world
Node JS as web server
• Import Required Module
var http = require("http");
• Create Server
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});

// Send the response body as "Hello World"


response.end('Hello World\n');
}).listen(8081);

// Console will print the message


console.log('Server running at http://127.0.0.1:8081/');
Browser
Mongo DB

• MongoDB is a cross-platform, document oriented database that


provides, high performance, high availability, and easy scalability.
• MongoDB works on concept of collection and document.
• Database
• Database is a physical container for collections. Each database gets its own
set of files on the file system.
• A single MongoDB server typically has multiple databases.
Collection

• Collection
• Collection is a group of MongoDB documents. It is the equivalent of an
RDBMS table.
• A collection exists within a single database.
• Collections do not enforce a schema.
• Documents within a collection can have different fields.
• Typically, all documents in a collection are of similar or related purpose.
Document
• A document is a set of key-value pairs. Documents have dynamic schema
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
tags: ['mongodb', 'database', 'NoSQL'],
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
},
{
user:'user2',
message: 'My second comments',
dateCreated: new Date(2011,1,25,7,45),
}
]
}
Document

• id is a 12 bytes hexadecimal number which assures the uniqueness of


every document.
• You can provide _id while inserting the document. If you don’t
provide then MongoDB provides a unique id for every document.
• These 12 bytes first 4 bytes for the current timestamp, next 3 bytes
for machine id, next 2 bytes for process id of MongoDB server and
remaining 3 bytes are simple incremental VALUE.
Uses of mongoDB

• Document Oriented Storage − Data is stored in the form of JSON


style documents.
• Index on any attribute
• Replication and high availability
• Rich queries
• Fast in-place updates
Areas

• Big Data
• Content Management and Delivery
• Mobile and Social Infrastructure
• User Data Management
• Data Hub
Installation

• The Download Center should display MongoDB Community Server download information.
If not, select Server, then click the MongoDB Community Server tab.

• In the Version dropdown, select the version that corresponds to the latest MongoDB Server
4.2.

• In the OS dropdown, Windows 64-bit X64 should be selected.

• In the Package drop down, MSI should be selected.

• Click Download.

• Go to the directory where you downloaded the MongoDB installer (.msi file). By default,
this is your Downloads directory.

• Double-click the .msi file.


Mongo shell
Create DB, Collection

• use DATABASE_NAME – creates database


• Show dbs – list the database
• db.createCollection("mycollection") – create collection
• db.employee.insert({name:’VIT’,address:'vellore'}) – insert into
collection
CRUD operations

• For information on CRUD (Create,Read,Update,Delete) operations


• Insert Documents
• Query Documents
• Update Documents
• Delete Documents
Sample
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {


if (err) throw err;
console.log("Database created!");
db.close();
})
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");
dbo.createCollection("customers", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
Insert one

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");
var myobj = { name: "Company Inc", address: "Highway 37" };
dbo.collection("customers").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
Insert Many
var MongoClient = { name: 'Susan', address: 'One way 98'},
require('mongodb').MongoClient;
{ name: 'Vicky', address: 'Yellow Garden
var url = "mongodb://localhost:27017/"; 2'},
MongoClient.connect(url, function(err, { name: 'Ben', address: 'Park Lane 38'},
db) {
{ name: 'William', address: 'Central st
if (err) throw err; 954'},
var dbo = db.db("mydb"); { name: 'Chuck', address: 'Main Road
var myobj = [ 989'},
{ name: 'John', address: 'Highway 71'}, { name: 'Viola', address: 'Sideway 1633'}
{ name: 'Peter', address: 'Lowstreet 4'}, ];
{ name: 'Amy', address: 'Apple st 652'},
{ name: 'Hannah', address: 'Mountain dbo.collection("customers").insertMany(my
21'}, obj, function(err, res) {
{ name: 'Michael', address: 'Valley if (err) throw err;
345'}, console.log("Number of documents
{ name: 'Sandy', address: 'Ocean blvd inserted: " + res.insertedCount);
2'}, db.close();
Find one

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");
dbo.collection("customers").findOne({}, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});
});
Find All

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");
dbo.collection("customers").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
Sort Result

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");
var mysort = { name: 1 };
dbo.collection("customers").find().sort(mysort).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
Limit Result

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");
dbo.collection("customers").find().limit(5).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy