Introduction to MEAN Stack

This resource provides overview about

Introduction to MEAN Stack
Author
Materio
Listen

[attachment:https://static-materio.vercel.app/common/files/attach/4f3001a2-d238-44ff-8108-afb6651fb90c.pdf:Introduction to MEAN Stack]

Data is raw facts, figures, symbols, or observations that by themselves have little or no meaning until they are organized and interpreted. In simple terms, it is the basic input from which useful information is produced.

  • Data can be numbers, text, images, audio, video, sensor readings, clicks, or any recordable details about events, objects, or people.
  • When this data is processed, structured, and given context (like summarizing sales by month), it becomes information that supports understanding and decision‑making.

Types of data

  • Quantitative data: Numerical and measurable values, such as age, temperature, sales, or number of users.
  • Qualitative data: Descriptive or non‑numerical details, like reviews, comments, categories, or labels (e.g., “good”, “poor”, “bug found”).

In computing

  • Computer data is information encoded in a machine‑readable form, usually binary (0s and 1s), so that computers can store, transmit, and process it efficiently.
  • Data is typically organized into structures such as tables, files, or databases to make it easier to query, analyze, and update.

information

Information is data that has been processed, organized, and given context so that it becomes meaningful and useful for understanding or decision‑making.

  • Information is structured or processed data that “makes sense” to the person or system using it.
  • It answers questions like who, what, where, when, and how by adding context to otherwise raw facts.

Relation to data

  • Data is raw facts (for example, individual test scores), while information is the result of processing that data into a meaningful form (for example, the class average).
  • Information depends on data; without underlying data, no information can be produced.

In computing

  • In computing, information is the useful output produced after a system inputs, stores, and processes raw data.
  • It can be represented digitally (binary) and shown as reports, tables, graphs, dashboards, or messages that support decisions.

database A database is an organized, structured collection of related data, stored electronically so that it can be efficiently accessed, managed, updated, and queried by users or applications.

  • A database groups data in a logical way (for example, tables of students, courses, and enrollments) so relationships between items are clear.
  • Specialized software called a Database Management System (DBMS) (like MySQL, PostgreSQL, Oracle) is used to create, store, retrieve, and secure this data.

Key features

  • Supports operations such as inserting, updating, deleting, and searching data quickly, even for very large datasets.
  • Often uses Structured Query Language (SQL) so developers and applications can query and manipulate data in a standardized way.

types

Hierarchical and network databases are early or “traditional” database models that organize data differently from modern relational databases. Both are mostly seen today in legacy or very specialized systems.

Hierarchical database model

  • Organizes data in a tree‑like structure with parent–child relationships; each child has exactly one parent, but a parent can have multiple children (one‑to‑many).
  • Suits data that naturally forms hierarchies (like organization charts or file systems), but is rigid and hard to change or use for complex many‑to‑many relationships.

Network database model

  • Extends the hierarchical idea by allowing a child record to have multiple parents, representing many‑to‑many relationships using a graph‑like structure with records and links/pointers.
  • More flexible than hierarchical databases for complex relationships, but the structure and navigation are more complex to design, program, and maintain.

Quick comparison

AspectHierarchical DBNetwork DB
StructureTree (parent → child)Graph with records and links
RelationshipsOne‑to‑one, one‑to‑manyOne‑to‑one, one‑to‑many, many‑to‑many
Parent of a nodeExactly one parentOne or multiple parents
FlexibilityRigid, less flexibleMore flexible but complex
Typical usage nowLegacy, niche hierarchical dataLegacy, specialized complex models

Databases come in two main categories: relational (SQL) and non‑relational (NoSQL), each suited for different data needs and workloads. Relational databases emphasize structured data and ACID guarantees, while NoSQL handles unstructured or semi‑structured data at massive scale.

Relational databases (SQL)

  • Store data in fixed tables with rows and columns, linked by keys (primary and foreign keys) to enforce relationships and consistency. geeksforgeeks
  • Use SQL for queries, updates, and schema enforcement; examples include MySQL, PostgreSQL, Oracle, SQL Server. oracle
  • Best for transactions needing accuracy (banking, inventory) where data fits a predefined structure. techtarget

Non‑relational databases (NoSQL)

  • Flexible designs without fixed schemas; data stored in documents, key‑value pairs, graphs, or wide columns for variety and speed. rivery
  • Categories include document (MongoDB), key‑value (Redis), column‑family (Cassandra), graph (Neo4j); often use custom query languages. thoughtspot
  • Ideal for big data, real‑time apps (social media, IoT), or when structure changes often, prioritizing speed and scalability over strict consistency. rivery

Quick comparison

AspectRelational (SQL)Non‑relational (NoSQL)
Data structureTables, rows, columns, schemas techtargetDocuments, key‑value, graphs, flexible rivery
Query languageSQL (standardized) oracleVaried (JSON, custom APIs) rivery
ScalabilityVertical (bigger servers) techtargetHorizontal (add servers easily) thoughtspot
Best forTransactions, complex queries aws.amazonBig data, speed, variety rivery
ConsistencyACID (strong) en.wikipediaBASE (eventual) rivery

NoSQL databases offer key advantages over traditional SQL ones, especially for handling large, varied, or rapidly changing data in modern apps. They shine in flexibility, speed, and scaling for big data scenarios like social media or IoT.

Flexible schema

  • No fixed structure upfront, so you can store structured, semi‑structured, or unstructured data (like JSON documents) and evolve schemas dynamically without downtime or migrations.
  • Great for agile development where data needs change quickly, saving time on redesigns.

Horizontal scalability

  • Easily scale out by adding cheap servers (nodes) to distribute data and workload, handling massive growth without slowing down.
  • Ideal for high‑traffic apps, unlike SQL's vertical scaling which needs pricier hardware upgrades.

High performance

  • Optimized for fast reads/writes on big datasets, often without complex joins, using denormalized data for low latency.
  • Supports real‑time processing and huge volumes, perfect for analytics or user feeds.

NoSQL databases have surged in popularity due to their ability to handle the explosive growth of big data, web apps, and cloud environments that traditional SQL struggles with. Their design matches modern needs like speed, flexibility, and massive scale.

Big data explosion

  • The internet generates huge volumes of varied data (user posts, logs, sensors) that NoSQL processes faster and cheaper than SQL for petabyte‑scale workloads.
  • Adopted by giants like Google, Facebook, Netflix for handling millions of users and real‑time updates.

Web and cloud era

  • Perfect for distributed systems and microservices in cloud platforms (AWS DynamoDB, MongoDB Atlas), scaling horizontally across cheap servers.
  • Rise of mobile, social media, and IoT created needs for flexible, schema‑less storage that NoSQL provides natively.

Developer friendly

  • Agile development: Change schemas on the fly without breaking apps, speeding up iteration in startups and DevOps.
  • Simpler for unstructured data like JSON, cutting learning curves and boilerplate compared to rigid SQL schemas.

NoSQL databases are grouped into four main types: document‑based, key‑value, column‑family (wide‑column), and graph, each optimized for specific data patterns and use cases.

Document‑based

  • Stores data as flexible, self‑contained documents (like JSON or BSON) that can have varying fields, ideal for semi‑structured data.
  • Examples: MongoDB (stores product catalogs, user profiles), CouchDB.
  • Best for content management, e‑commerce, or apps with evolving schemas.

Key‑value

  • Simplest type: Data as unique keys mapped to simple or complex values (strings, blobs), super fast for lookups.
  • Examples: Redis (caching, sessions), DynamoDB, Riak.
  • Perfect for session storage, leaderboards, or real‑time apps needing speed.

Column‑family (wide‑column)

  • Organizes data by columns grouped into families, not rows; great for sparse data and analytics.
  • Examples: Cassandra (handles time‑series, IoT, logs), HBase.
  • Suited for big data analytics, monitoring, or write‑heavy apps.

Graph

  • Models data as nodes (entities), edges (relationships), and properties; excels at connected data.
  • Examples: Neo4j (social networks, fraud detection), Amazon Neptune.
  • Ideal for recommendations, networks, or queries like “friends of friends”.

MongoDB is a popular open‑source document‑oriented NoSQL database that stores data in flexible JSON‑like documents (BSON format), making it easy to scale and develop modern apps. Launched in 2009, it powers companies like Google, Adobe, and Forbes for handling diverse, high‑volume data.

Core structure

  • Documents: Basic units like JSON objects with key‑value pairs (e.g., {"name": "Raj", "age": 20, "city": "Rajkot"}); supports nesting and arrays.
  • Collections: Groups of documents, like tables but schema‑free (documents in one collection can differ).
  • Databases: Containers holding multiple collections.

Key features

  • Schema flexibility: Add or change fields anytime without migrations.
  • Horizontal scaling: Shard data across servers for massive growth; replication for high availability.
  • Query power: Rich queries, indexing, aggregation pipelines for analytics (like SQL GROUP BY).
  • ACID support: Multi‑document transactions since v4.0 for reliability.

Real‑life analogy

Think of MongoDB as a digital notebook where each page (document) holds related info freely—no rigid columns—perfect for apps like blogs (posts with varying tags/images) or e‑commerce (products with optional specs).

Exam tip

Mnemonic: "Mongo = Massive, Open, NoSQL, Great for Objects" – remember it stores objects natively, unlike tables in SQL. Practice inserting a document: db.students.insertOne({name: "Ali", marks: 85}).

MongoDB's internals revolve around BSON serialization, WiredTiger storage engine, and flexible document storage, differing from MySQL's rigid table‑based rows. Here's a simple breakdown with direct mappings.

Data format: JSON to BSON

  • Starts as JSON (human‑readable: {"name": "Raj", "marks": 85}) but converts to BSON (binary JSON) for efficient storage and querying.
  • BSON adds types like int32 (32‑bit integers, e.g., small counts), int64 (64‑bit for big numbers like timestamps), doubles, strings, ObjectIds, dates, arrays—compact and fast.
  • Mapping: MySQL stores mixed types in flexible columns; MongoDB embeds everything in documents for speed.

Storage engine: WiredTiger

  • Default engine since MongoDB 3.2: Handles reads/writes with document‑level locking (finer than MySQL's row/table locks), compression (Snappy), and checkpoints for crash recovery.
  • Internals: Data in B‑tree files (e.g., collection‑0‑XXXX.wt), catalog (_mdb_catalog.wt), history store (WiredTigerHS.wt) for MVCC snapshots.
  • Read/write: Optimistic concurrency; writes append to logs (WAL), reads from cache/disk. Scales with filesystem cache.

MySQL vs MongoDB mapping

MySQL conceptMongoDB equivalentKey difference
TablesCollectionsSchema‑free; mix document shapes
RowsDocuments (BSON objects)Nested/embedded; no fixed order
ColumnsFields (key‑value pairs)Dynamic; arrays/objects inside
InnoDBWiredTigerDocument locking vs row locking
SQL queryMongo query (JSON filters)No joins; use aggregation pipelines

Real‑life tip

Imagine MySQL as spreadsheet rows (fixed columns)—great for reports. MongoDB is like nested folders (documents with sub‑files)—flexible for apps like chats (messages with attachments). Mnemonic: "BSON = Binary Super Objects, Not tables". Test: Insert db.users.insertOne({name: "Ali", scores: [90, 85]})—see int32 arrays in BSON dump!

MongoDB uses simple shell commands (in mongosh) to create and manage databases/collections—databases are created implicitly on first use. Here's a step‑by‑step table of essential commands, starting from setup.

Essential MongoDB Commands Table

Step/ActionCommandExample / Notes
Connect & show DBsshow dbsLists all databases (needs data to show).
Switch/Create DBuse mydbSwitches to mydb; creates if new (implicit).
Create Collectiondb.createCollection("students")Explicitly creates empty collection.
Insert One Documentdb.students.insertOne({name: "Raj", age: 20})Adds single BSON doc; auto‑creates collection/DB.
Insert Manydb.students.insertMany([{name: "Ali"}, {name: "Priya"}])Batch insert; ordered by default.
Show Collectionsshow collections or db.getCollectionNames()Lists collections in current DB.
Find Alldb.students.find()Shows all docs (use .pretty() for nice format).
Find with Filterdb.students.find({age: {$gt: 18}})Age > 18; operators like $eq, $in, $regex.
Update Onedb.students.updateOne({name: "Raj"}, {$set: {marks: 85}})Updates first match; $set, $inc, $push.
Update Manydb.students.updateMany({age: {$lt: 21}}, {$set: {group: "A"}})Updates all matches.
Delete Onedb.students.deleteOne({name: "Ali"})Deletes first match.
Delete Manydb.students.deleteMany({marks: {$lt: 50}})Deletes all low marks.
Drop Collectiondb.students.drop()Deletes entire collection.
Drop DBdb.dropDatabase()Deletes current DB (careful!).

Quick start sequence

use school          # Switch/create DB
db.students.insertOne({name: "Raj", city: "Rajkot"})  # Creates collection + data
db.students.find().pretty()  # View data nicely
show collections    # List in DB

Tip: No CREATE DATABASE like SQL—use + insert does it. Mnemonic: "Use DB, Insert Doc, Find Fun!" Practice in free MongoDB Atlas playground for exams.

Here’s a table of additional MongoDB commands (excluding basic CRUD like insertOne/Many, find, updateOne/Many, deleteOne/Many from before). These cover advanced ops like findAndModify (atomic find+replace), upsert, indexing, aggregation, and more for exams/practice.

Advanced MongoDB Commands Table

Command CategoryCommandExample / Notes
Find & Modifydb.students.findOneAndUpdate({name: "Raj"}, {$set: {marks: 90}}, {returnDocument: "after"})Atomic find+update; returns old ("before") or new ("after") doc. Use for counters.
Upsertdb.students.updateOne({name: "New"}, {$set: {marks: 95}}, {upsert: true})Update if exists, insert if not; great for "set if absent".
Replacedb.students.replaceOne({name: "Raj"}, {name: "Raj", marks: 95, grade: "A"})Fully replaces matching doc (loses extra fields); not partial like update.
Countdb.students.countDocuments({marks: {$gt: 80}})Counts matching docs; faster than find().count().
Distinctdb.students.distinct("city")Returns unique values for a field (e.g., ["Rajkot", "Ahmedabad"]).
Create Indexdb.students.createIndex({name: 1})Speeds queries; 1 ascending, -1 descending. Compound: {name:1, marks:-1}.
List Indexesdb.students.getIndexes()Shows all indexes on collection.
Drop Indexdb.students.dropIndex("name_1")Drops specific index (name from getIndexes).
Aggregationdb.students.aggregate([{$match: {marks: {$gt: 80}}}, {$group: {_id: "$city", avg: {$avg: "$marks"}}}])Pipeline for GROUP BY, SUM, etc.; like SQL analytics.
Limit & Sortdb.students.find().sort({marks: -1}).limit(5)Top 5 by marks descending; chain with skip for pagination.
Projectiondb.students.find({}, {name:1, marks:1, _id:0})Select only fields (1=include, 0=exclude); slim results.
Drop Index Alldb.students.dropIndexes()Removes all indexes except default _id.

Exam tricks

  • findOneAndUpdate vs updateOne: Atomic + returns doc (use for likes/votes).
  • Upsert mnemonic: "Up‑SERT = Update or iNSERT".
  • Index tip: Always index queried fields (name, email); slows inserts but speeds finds 100x. Practice aggregation for "average marks by city"!

Schema design defines the structure and rules for data storage (tables/fields in SQL, documents in MongoDB), while data modeling is the broader process of planning how data relates and flows to fit app needs. Schema is the "blueprint"; modeling decides the blueprint.

Schema Design

  • Focuses on physical structure: Field names, types, constraints, indexes (SQL enforces rigidly; MongoDB flexible).
  • In SQL (MySQL): CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50)).
  • In MongoDB: Optional validation like db.createCollection("students", {validator: { $jsonSchema: { bsonType: "object", required: ["name"], properties: {name: {bsonType: "string"}}}}}).

Data Modeling

  • Conceptual/logical planning: Identify entities, relationships (one‑to‑many?), access patterns, then choose embed/reference.
  • Questions: Read/write ratio? Embed for reads (fast, no joins) or reference for writes (avoid duplication)?
  • MongoDB example: Model blog post with comments embedded (array of comment docs) for quick full loads.

Key differences

AspectSchema DesignData Modeling
FocusStructure, validation, storage stackoverflowRelationships, queries, app workflow projectmanagementplanet
WhenImplementation step dbschemaEarly planning, iterative mongodb
SQLRigid, upfront DDL dbschemaNormalized (3NF), joins projectmanagementplanet
MongoDBFlexible, runtime mongodbEmbedding/referencing patterns mongodb
OutputTables/collections ready stackoverflowER diagrams, patterns mongodb

Real‑life example

E‑commerce: Modeling decides "embed product reviews in product doc?" (fast reads). Schema then defines fields like {reviews: [{user: string, rating: int32}]}. Tip: For exams, remember "Model first (why), Schema second (how)"—MongoDB flips SQL's rigid order!

MongoDB data modeling uses two main approaches for relationships: embedded documents (store data inside one doc) and referencing (link to another collection via IDs). Choose based on access patterns, growth, and the 16MB doc limit—reference when embedding would exceed it or bloat docs.

Embedded documents

  • Related data lives inside the parent document (arrays/objects), fetched in one query—fast reads, atomic updates.
  • Example: Blog post with comments:
    {
      "_id": "...",
      "title": "Mongo Tips",
      "comments": [{ "user": "Raj", "text": "Great!" }, { "user": "Ali", "text": "Thanks" }]
    }
    
  • Best when: 1:few (small arrays), always read together, rarely updated separately.

Referencing documents

  • Store related data in separate collection, link with ObjectId refs—multiple queries but avoids duplication and size limits.
  • Example: Separate comments collection:
    // posts collection
    { "_id": ObjectId("..."), "title": "Mongo Tips" }
    // comments collection  
    { "_id": "...", "postId": ObjectId("..."), "user": "Raj", "text": "Great!" }
    
  • Query: db.comments.find({postId: ObjectId("...")}).

When to use each

ScenarioEmbedReference
Relationship1:1 or 1:few (comments <100)1:many or many:many (viral posts, users→posts)
AccessAlways together (read profile+address)Independent (query comments alone)
UpdatesRare on child (static bio)Frequent/child grows (user edits comments often)
Size limit hitNo (under 16MB)Yes—split large arrays/docs
MongoDB doc maxStays smallEach doc flexible, no single‑doc bloat

Pro tip & mnemonic

  • Rule: "Together forever? Embed. Growing giants? Reference."
  • 16MB trap: Viral thread (1000+ comments)? Reference or bucket (sub‑arrays by date). Test: Embed grows → db.posts.updateOne({}, {$push: {comments: {...}}}) fails at limit!

MongoDB Aggregation Framework is a powerful pipeline for advanced data processing—like SQL's GROUP BY, JOINs, analytics—but on documents, using stages chained in an array. Syntax: db.collection.aggregate([ { $stage1 }, { $stage2 } ]).

Key pipeline stages

StagePurpose & Syntax ExampleLike SQL Equivalent
$matchFilter docs early (boosts speed) { $match: {age: {$gt: 18}} }WHERE clause
$projectSelect/reshape fields { $project: {name:1, marks:1, _id:0} }SELECT columns
$groupGroup & aggregate { $group: {_id: "$city", avgMarks: {$avg: "$marks"}, count: {$sum:1}} }GROUP BY, AVG(), COUNT()
$sortSort results { $sort: {marks: -1} } (1 asc, -1 desc)ORDER BY
$limit / $skipLimit results { $limit: 10 }, { $skip: 20 }LIMIT, OFFSET
$unwindFlatten arrays { $unwind: "$skills" }Normalize arrays
$lookupJoin collections { $lookup: {from: "comments", localField: "_id", foreignField: "postId", as: "comments"} }LEFT JOIN

Full example: Average marks by city

db.students.aggregate([
  { $match: {marks: {$gte: 50}} },      // Filter pass
  { $group: {_id: "$city", avg: {$avg: "$marks"}, students: {$sum: 1}}},  // Group calc
  { $sort: {avg: -1} },                 // Top first
  { $project: {city: "$_id", avg:1, students:1, _id:0} }  // Rename/format
])

Output: [{city: "Rajkot", avg: 82.5, students: 5}, ...].

Pro tips

  • Order matters: $match first, $group before $sort.
  • Operators: $sum, $avg, $min, $max, $push (collect array), $addToSet (unique).
  • Mnemonic: "Match Project Group Sort Limit = M PGSL pipeline flow". Practice for exams: Compute "top 3 cities by avg marks >75"!