Skip to main content
MongoDB & Mongoose
An End-to-End Overview of NoSQL Data Storage, Schema Modeling, and Node.js Integration
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
01. Database Fundamentals
Understanding modern data management and NoSQL database architecture
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
What is MongoDB?
NoSQL Document Store
MongoDB is a popular NoSQL database that bypasses
traditional rigid SQL tables. It stores data in flexible, JSON-
like document formats called BSON.
Designed for rapid iteration and scalability in modern web
development.
Key Features
Schema-less: Structure documents dynamically without
fixed columns.
Scalable: Supports high throughput via horizontal
sharding.
High Performance: Optimized for high-speed indexing and
indexing capabilities.
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
MongoDB vs. MySQL Comparison
Feature MongoDB (NoSQL) MySQL (Relational SQL)
Database Type Document-Oriented Database Relational Database Management System
Data Storage JSON / BSON Documents Structured Tables with Rows & Columns
Schema Structure Dynamic / Schema-less flexibility Strict pre-defined Schema required
Relationships Embedded Documents & References Foreign Keys and SQL JOIN Queries
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
How MongoDB Stores Data
Collections & Documents
Instead of SQL tables, MongoDB organizes records into Collections
containing JSON-formatted Documents.
"_id" "64e1b3f7f5a123abc789" "name" "Alice" "email"
"alice@example.com" "age" 25
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
Deployment & Setup Options
Local Installation
Download and run the MongoDB Community Edition on
your local developer workstation for offline development
and testing.
MongoDB Atlas (Cloud)
Fully managed cloud database hosting service. Creates
automated clusters with automated connection strings.
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
Introduction to Mongoose
Schema Modeling
Defines rigid or semi-rigid structures
and default properties for MongoDB
documents in Node.js.
Data Validation
Enforces required values, field data
types, data formats, and custom
error handling before writing.
Built-in Querying
Provides powerful helper methods
for seamless CRUD execution directly
within Node.js code.
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
Mongoose Connection
Connecting Node.js to MongoDB
Establish asynchronous connections safely using
mongoose.connect().
Best Practice: Always hide database passwords inside a .env
environment file.
const async try await
connect
log "MongoDB Connected..." catch
error exit 1
// Exit with failure
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
Schema vs. Model in Mongoose
Mongoose Schema
The Blueprint of your document.
Defines the structure, field data types, validation rules,
default values, and methods for individual data records.
Mongoose Model
The Interface / Toolbox for database actions.
A wrapper class created from a Schema that executes
database CRUD operations on a target MongoDB
collection.
const new
const model 'User'
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
Defining Schema & Rules
Type Declarations: Define strict JavaScript primitive types (e.g., String, Number, Boolean, Date).
Required & Unique Options: Enforce mandatory fields and uniquely indexed data across collections.
Enum Restraints: Restrict text input strictly to an array of approved string values.
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
Mongoose CRUD Operations
Create
Instantiate document and persist to
database.
Read
Query documents matching criteria.
Update
Locate by ID and apply changes.
const ne
w
await save
const await
find 'admin'
await findByIdAndUpdate
true
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE
Summary & Questions
MongoDB & Mongoose simplify scalable NoSQL backend development in Node.js applications.
Dr. N. JUBER RAHMAN, AP, SRCAS - CBE