Unit-3
Advanced Node JS and Database: Introduction to NoSQL databases – MongoDB system overview - Basic querying
with MongoDB shell – Request body parsing in Express – NodeJS MongoDB connection – Adding and retrieving data
to MongoDB from NodeJS – Handling SQL databases from NodeJS – Handling Cookies in NodeJS – Handling User
Authentication with NodeJS.
🔹 1. Introduction to NoSQL Databases
✅ What is NoSQL?
NoSQL stands for "Not Only SQL". It refers to a new generation of databases that:
Do not use relational models
Do not require fixed schemas
Are designed for scalability, flexibility, and high performance
🧠 Why NoSQL?
Traditional SQL databases (like MySQL, PostgreSQL) are great for structured data with relationships, but
they:
Can become slow and complex with large, unstructured data
Require predefined schemas
Are harder to scale horizontally (across multiple servers)
NoSQL solves these problems with:
Dynamic schemas
High speed for read/write
Easy horizontal scaling
📂 Types of NoSQL Databases
Type Description Example
Document Stores data as JSON-like documents MongoDB, CouchDB
Key-Value Stores data as key-value pairs (like a dictionary) Redis, DynamoDB
Column-Family Stores data in columns instead of rows, great for big data Cassandra, HBase
Graph Stores nodes and relationships (edges) between them Neo4j, ArangoDB
🧠 Example: Document Model (MongoDB)
{
"_id": "abc123",
"name": "John Doe",
"age": 30,
"email": "john@[Link]",
"address": {
"city": "New York",
"zip": "10001"
}
}
This is one document inside a collection called users.
Unlike SQL, you don’t need to define a fixed schema for these documents.
⚙️ Main Features of NoSQL:
🔄 Schema-less: Structure can change on the fly
⚡ Faster reads/writes: Ideal for real-time apps
🧩 Scalable: Easy to distribute across multiple servers (horizontal scaling)
🌐 Cloud-friendly: Used in big companies like Google, Facebook, Amazon
📌 When to Use NoSQL:
You deal with large volumes of unstructured or semi-structured data
You want to store data in flexible formats (like JSON)
You need fast development without strict schema constraints
You’re building a real-time or high-performance application
🔹 2. MongoDB System Overview
🧠 What is MongoDB?
MongoDB is a popular NoSQL database that stores data in JSON-like documents (called BSON – Binary
JSON). It’s designed for:
High performance
High availability
Easy scalability
🧠 Key Concepts
Term Description
Database The top-level container for all data (like mydb)
Collection A group of documents (like a SQL table, e.g., users)
Document A single record in a collection (like a SQL row), stored in BSON/JSON format
Field A key-value pair inside a document (like a column in SQL)
🧠 Data Structure Example
{
"_id": "abc123",
"name": "Alice",
"email": "alice@[Link]",
"skills": ["[Link]", "MongoDB"],
"profile": {
"age": 28,
"location": "Mumbai"
}
}
This is one document inside a collection (e.g., users).
⚙️ MongoDB Architecture
1. Client: Application (like [Link]) that sends queries.
2. MongoDB Server: The database engine that processes requests.
3. Database: Stores multiple collections.
4. Collections: Store multiple JSON-like documents.
5. Documents: Contain actual data.
💡 Main Features
📄 Document-Oriented: Flexible structure (no fixed schema)
🚀 High Performance: Fast read/write operations
📶 Horizontal Scaling: Easy to scale with sharding
🛡️ Replication: Ensures high availability with replica sets
🔍 Powerful Querying: Supports rich queries using JavaScript-like syntax
🛠 Tools You Can Use
Tool Purpose
Mongo Shell Command-line tool to interact with MongoDB
MongoDB Compass GUI interface for exploring and managing data
Mongoose ODM (Object Data Modeling) library for [Link]
🔐 BSON vs JSON
MongoDB stores data in BSON (Binary JSON) internally.
BSON adds data types like Date, ObjectId, etc., which are not part of standard JSON.
🧠 Use Cases
Social media applications
Real-time analytics
IoT applications
Content management systems
E-commerce product catalogs
🔹 3. Basic Querying with MongoDB Shell
The MongoDB Shell (mongosh) is an interactive JavaScript interface where you can interact with your MongoDB
databases and collections.
✅ Start the MongoDB Shell
To start the shell:
mongosh
You can then switch to or create a database:
use myDatabase
📁 Basic Collection Operations
📌 1. Insert Documents
[Link]({
name: "Alice",
age: 25,
email: "alice@[Link]"
})
[Link]([
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 }
])
🔍 2. Find Documents
[Link]() // Find all
[Link]({ age: 30 }) // Filter by age
[Link]({ name: "Bob" }) // Find one
🎯 3. Query Operators
[Link]({ age: { $gt: 28 } }) // Greater than
[Link]({ age: { $lte: 30 } }) // Less than or equal to
[Link]({ name: { $in: ["Alice", "Bob"] } }) // In array
✍️ 4. Update Documents
[Link](
{ name: "Alice" },
{ $set: { age: 26 } }
)
[Link](
{ age: { $lt: 30 } },
{ $inc: { age: 1 } }
)
❌ 5. Delete Documents
[Link]({ name: "Charlie" })
[Link]({ age: { $gt: 30 } })
📊 Projection (Select Fields)
[Link]({}, { name: 1, _id: 0 }) // Only show name field
🔢 Sorting & Limiting
[Link]().sort({ age: 1 }) // Ascending order
[Link]().sort({ age: -1 }) // Descending order
[Link]().limit(2) // Limit number of results
🧠 Count Documents
[Link]()
[Link]({ age: { $gte: 30 } })
🔹 4. Request Body Parsing in Express
When building a web app using [Link] + Express, we often send data from the client (browser) to the server — for
example, via a form or API request. This data is known as the request body.
To access this body data in Express, we use a body parser.
✅ What is Body Parsing?
Body parsing means reading the incoming data from the client and converting it into a format (like JSON or URL-
encoded) that your [Link] app can use.
📦 Step-by-Step Setup in Express
🔹 1. Install Express
npm install express
🔹 2. Create Basic Express App
const express = require('express');
const app = express();
const port = 3000;
🔹 3. Enable Body Parsing Middleware
From Express v4.16+, you can use [Link]() and [Link]() directly (no need to install
body-parser separately).
[Link]([Link]()); // For parsing application/json
[Link]([Link]({ extended: true })); // For parsing application/x-www-form-
urlencoded
📥 Example: Handling JSON Body
🔹 Client Sends:
{
"name": "Alice",
"age": 25
}
🔹 Server Code:
[Link]('/user', (req, res) => {
const userData = [Link];
[Link](userData); // Output: { name: 'Alice', age: 25 }
[Link]('User data received');
});
📄 Example: Form Data (URL-Encoded)
🔹 HTML Form:
<form action="/submit" method="POST">
<input name="username" />
<input name="email" />
<button type="submit">Submit</button>
</form>
🔹 Server Code:
[Link]('/submit', (req, res) => {
const { username, email } = [Link];
[Link](`Received: ${username}, ${email}`);
});
⚠️ Without Body Parser?
If you don't use body parsing middleware:
[Link] will be undefined for POST requests.
✅ Summary
Middleware Parses Usage
[Link]() application/json JSON APIs
[Link]() application/x-www-form-urlencoded HTML form submissions
🔹 5. [Link] MongoDB Connection
To connect your [Link] app to MongoDB, you’ll use the official mongodb driver or an ORM like mongoose. We’ll
cover both methods below — starting with the basic driver.
✅ Method 1: Using MongoDB Native Driver
🔹 Step 1: Install MongoDB Driver
npm install mongodb
🔹 Step 2: MongoDB Connection Code
const { MongoClient } = require('mongodb');
const uri = "mongodb://localhost:27017"; // replace with your MongoDB URI
const client = new MongoClient(uri);
async function run() {
try {
await [Link](); // connect to MongoDB
[Link]("✅ Connected to MongoDB");
const database = [Link]("myDatabase");
const collection = [Link]("users");
// Example insert
await [Link]({ name: "Alice", age: 25 });
[Link]("📥 Data inserted");
// Example find
const users = await [Link]().toArray();
[Link]("👥 Users:", users);
} catch (err) {
[Link]("❌ Error:", err);
} finally {
await [Link](); // close connection
}
}
run();
✅ Method 2: Using Mongoose (Recommended for Express apps)
🔹 Step 1: Install Mongoose
npm install mongoose
🔹 Step 2: Mongoose Connection Code
const mongoose = require('mongoose');
[Link]('mongodb://localhost:27017/myDatabase', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => [Link]("✅ Connected to MongoDB"))
.catch(err => [Link]("❌ MongoDB connection error:", err));
🔹 Step 3: Create a Mongoose Model
const User = [Link]("User", {
name: String,
age: Number
});
🔹 Step 4: Insert and Read Data
// Add data
const newUser = new User({ name: "Bob", age: 30 });
await [Link]();
// Get data
const users = await [Link]();
[Link](users);
🧠 When to Use Which?
Method Use Case
mongodb driver Lightweight and flexible (good for custom solutions)
mongoose Schema-based, great with Express, easy to manage models
🔹 6. Adding and Retrieving Data from MongoDB using [Link]
We’ll cover this with Mongoose (the most popular way in [Link] for working with MongoDB). If you prefer the
native MongoDB driver, let me know!
✅ Step-by-Step: Mongoose + Express Example
🔹 1. Install Packages
npm init -y
npm install express mongoose
🔹 2. Setup Folder Structure
project/
├── [Link]
└── models/
└── [Link]
🔹 3. Create User Model (models/[Link])
const mongoose = require('mongoose');
const userSchema = new [Link]({
name: String,
age: Number,
email: String
});
[Link] = [Link]('User', userSchema);
🔹 4. Server Setup ([Link])
const express = require('express');
const mongoose = require('mongoose');
const User = require('./models/User');
const app = express();
// Middleware
[Link]([Link]());
// MongoDB Connection
[Link]('mongodb://localhost:27017/myDatabase', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => [Link]('✅ Connected to MongoDB'))
.catch(err => [Link]('❌ MongoDB Error:', err));
/* ✅ ROUTES */
// ➕ Add a new user
[Link]('/users', async (req, res) => {
try {
const newUser = new User([Link]);
const result = await [Link]();
[Link](201).json(result);
} catch (err) {
[Link](500).json({ error: [Link] });
}
});
// 🔍 Get all users
[Link]('/users', async (req, res) => {
try {
const users = await [Link]();
[Link](users);
} catch (err) {
[Link](500).json({ error: [Link] });
}
});
// 🔍 Get user by ID
[Link]('/users/:id', async (req, res) => {
try {
const user = await [Link]([Link]);
if (!user) return [Link](404).send('User not found');
[Link](user);
} catch (err) {
[Link](500).json({ error: [Link] });
}
});
const PORT = 3000;
[Link](PORT, () => [Link](`🚀 Server running on [Link]
📬 Testing the API
Use Postman or any REST client:
➕ POST /users
Body (JSON):
{
"name": "Alice",
"age": 25,
"email": "alice@[Link]"
}
🔍 GET /users
Returns all users.
🔍 GET /users/:id
Returns a user by ID.
🧠 Tip:
Always validate and sanitize data in real-world apps using libraries like:
npm install joi express-validator
🔹 7. Handling SQL Databases from [Link]
[Link] can work with SQL databases like MySQL, PostgreSQL, SQLite, etc.
Here, we’ll use MySQL as a popular example.
We’ll cover:
✅ Setting up MySQL
✅ Connecting with [Link]
✅ Performing CRUD operations
✅ Using mysql2 and sequelize (ORM)
✅ Option 1: Using mysql2 (Low-level direct queries)
🔹 Step 1: Install MySQL Package
npm install mysql2
🔹 Step 2: Connect to MySQL
const mysql = require('mysql2');
// Create connection
const connection = [Link]({
host: 'localhost',
user: 'root',
password: '', // Your MySQL password
database: 'testdb'
});
// Connect
[Link](err => {
if (err) throw err;
[Link]('✅ Connected to MySQL');
});
🔹 Step 3: Create Table (optional)
const createTable = `
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100)
)
`;
[Link](createTable, (err, result) => {
if (err) throw err;
[Link]('📦 Users table ready');
});
🔹 Step 4: Insert Data
const sql = "INSERT INTO users (name, email) VALUES (?, ?)";
const values = ["Alice", "alice@[Link]"];
[Link](sql, values, (err, result) => {
if (err) throw err;
[Link]('✅ User inserted with ID:', [Link]);
});
🔹 Step 5: Fetch Data
[Link]("SELECT * FROM users", (err, results) => {
if (err) throw err;
[Link]('👥 Users:', results);
});
✅ Option 2: Using Sequelize (ORM for SQL)
🔹 Step 1: Install Sequelize
npm install sequelize mysql2
🔹 Step 2: Setup Sequelize
const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize('testdb', 'root', '', {
host: 'localhost',
dialect: 'mysql'
});
// Define model
const User = [Link]('User', {
name: [Link],
email: [Link]
});
// Sync database and use
(async () => {
try {
await [Link]();
[Link]('✅ Connected to MySQL using Sequelize');
await [Link](); // creates table if not exists
// Insert
await [Link]({ name: "Bob", email: "bob@[Link]" });
// Read
const users = await [Link]();
[Link]("👥 Users:", [Link](u => [Link]()));
} catch (error) {
[Link]('❌ Connection failed:', error);
}
})();
🧠 When to Use What?
Method Best For
mysql2 Simple queries and full control
Sequelize Clean code, modeling, scalability
🔹 8. Handling Cookies in [Link]
Cookies are small pieces of data stored on the client (browser) and sent to the server with each request. In
[Link] (especially with Express), cookies are commonly used for:
Storing session info
Keeping users logged in
Tracking user activity
✅ We'll cover:
1. Installing middleware
2. Setting cookies
3. Reading cookies
4. Deleting cookies
🔹 Step 1: Install cookie-parser
npm install cookie-parser
🔹 Step 2: Setup Express with Cookie Parser
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
[Link](cookieParser());
[Link]([Link]()); // for JSON body parsing
🔹 Step 3: Set a Cookie
[Link]('/set-cookie', (req, res) => {
[Link]('username', 'Alice', {
maxAge: 24 * 60 * 60 * 1000, // 1 day
httpOnly: true // prevents JS access to cookie
});
[Link]('✅ Cookie has been set!');
});
🔹 Step 4: Read a Cookie
[Link]('/get-cookie', (req, res) => {
const username = [Link];
[Link](`👤 Hello, ${username || 'Guest'}`);
});
🔹 Step 5: Delete a Cookie
[Link]('/delete-cookie', (req, res) => {
[Link]('username');
[Link]('🧹 Cookie deleted');
});
🔍 Full Example
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
[Link](cookieParser());
[Link]('/', (req, res) => {
[Link]('🍪 Cookie Demo Home');
});
[Link]('/set', (req, res) => {
[Link]('user', 'JohnDoe', { maxAge: 900000, httpOnly: true });
[Link]('✅ Cookie set!');
});
[Link]('/get', (req, res) => {
[Link](`🔍 Cookie value: ${[Link]}`);
});
[Link]('/delete', (req, res) => {
[Link]('user');
[Link]('🗑️ Cookie deleted');
});
[Link](3000, () => [Link]('🚀 Server running on [Link]
🧠 Notes:
httpOnly: Helps prevent client-side script access to the cookie.
secure: Only sends cookie over HTTPS.
You can use cookies for authentication, themes, preferences, etc.
🔹 9. Handling User Authentication with [Link]
User authentication is critical to verify users’ identity and protect resources.
We’ll cover a simple example using:
Express for server
bcrypt for password hashing
jsonwebtoken (JWT) for token-based authentication
MongoDB + Mongoose for user data storage
🔹 Why JWT?
Stateless, no session storage needed on server
Tokens sent with requests for protected routes
✅ Step 1: Install dependencies
npm install express mongoose bcrypt jsonwebtoken dotenv
🔹 Step 2: Basic User Schema (models/[Link])
const mongoose = require('mongoose');
const userSchema = new [Link]({
username: { type: String, required: true, unique: true },
password: { type: String, required: true }
});
[Link] = [Link]('User', userSchema);
🔹 Step 3: Server Setup with Auth Routes ([Link])
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const User = require('./models/User');
const app = express();
[Link]([Link]());
[Link]('mongodb://localhost:27017/authDemo', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => [Link]('✅ Connected to MongoDB'))
.catch(err => [Link](err));
// Register
[Link]('/register', async (req, res) => {
const { username, password } = [Link];
// Check if user exists
const existingUser = await [Link]({ username });
if (existingUser) return [Link](400).json({ message: 'Username already exists'
});
// Hash password
const salt = await [Link](10);
const hashedPassword = await [Link](password, salt);
// Save user
const user = new User({ username, password: hashedPassword });
await [Link]();
[Link](201).json({ message: 'User registered successfully' });
});
// Login
[Link]('/login', async (req, res) => {
const { username, password } = [Link];
// Find user
const user = await [Link]({ username });
if (!user) return [Link](400).json({ message: 'Invalid username or password' });
// Validate password
const validPass = await [Link](password, [Link]);
if (!validPass) return [Link](400).json({ message: 'Invalid username or password'
});
// Create and assign token
const token = [Link]({ _id: user._id }, [Link].TOKEN_SECRET, { expiresIn: '1h'
});
[Link]('auth-token', token).json({ token });
});
// Middleware to verify token
function auth(req, res, next) {
const token = [Link]('auth-token');
if (!token) return [Link](401).json({ message: 'Access Denied' });
try {
const verified = [Link](token, [Link].TOKEN_SECRET);
[Link] = verified;
next();
} catch (err) {
[Link](400).json({ message: 'Invalid Token' });
}
}
// Protected route example
[Link]('/dashboard', auth, (req, res) => {
[Link]({ message: `Welcome user ${[Link]._id}` });
});
const PORT = 3000;
[Link](PORT, () => [Link](`🚀 Server running on [Link]
🔹 Step 4: Create .env file in your project root
TOKEN_SECRET=yourSecretKeyHere123!
Replace with a strong secret key.
📬 How to Test
Register with POST /register sending JSON { "username": "alice", "password":
"mypassword" }
Login with POST /login, get token from response
Use token in header auth-token to access protected /dashboard route
🧠 Notes:
Never store passwords in plain text
Use HTTPS in production
For bigger apps, consider refresh tokens and OAuth2