CSE494Semester 7 • 4 CreditsElective Subjects

Intelligent Nosql Databases

This course provides an overview of NoSQL databases and MongoDB fundamentals for handling scalable, schema-flexible data. Students learn data modeling, CRUD operations, indexing, aggregation, application integration, AI-based predictive analytics, and development of a functional mini-project.

6 Units • Notes • Model Answers • MCQs
Select Course Unit (6 Units)
Active: Unit 1
Unit 1:Unit 1: Introduction to NoSQL and MongoDB
7 min read•High-Yield Unit Guide

Unit 1: Introduction to NoSQL and MongoDB

Unit 1 Exam Focus & Core Takeaways

Review the core definitions, standard architectures, and syntax patterns below. Pay special attention to sections highlighted with bullet points for your mid-term and end-term subjective questions.

Unit 1: Foundations of NoSQL Distributed Databases and MongoDB Architecture


1. Executive Summary & Paradigm Shift

For more than four decades, the Relational Database Management System (RDBMS) governed by Structured Query Language (SQL) and Codd's relational algebra served as the unquestioned cornerstone of enterprise data architecture. However, the explosion of Web 2.0 applications, cloud computing, social graphs, mobile sensors, and big data telemetry exposed fundamental architectural constraints in relational databases:

  • Rigid Schema Constraints: Inability to adapt to polymorphic, semi-structured, or rapidly evolving data without expensive schema migrations.
  • Vertical Scaling Ceiling (Scale-Up): Relying on increasingly expensive multi-socket hardware rather than scaling horizontally across commodity hardware.
  • Impedance Mismatch: The friction between object-oriented programming domains (classes, polymorphism, nested objects) and flat tabular relational models (requiring complex Object-Relational Mappings).
  • Distributed Latency of Joins: The exponential computational cost of computing multi-table relational joins across distributed network nodes.
CODE
+---------------------------------------------------------------------------------------------------+
|                                  THE DISTRIBUTED DATA PARADIGM                                    |
+---------------------------------------------------------------------------------------------------+
|  TAXONOMY OF NOSQL DATA MODELS         DISTRIBUTED CONSISTENCY          MONGODB INTERNALS         |
|  - Document Stores (MongoDB/CouchDB)   - CAP Theorem (Brewer / Gilbert) - BSON Binary Format      |
|  - Key-Value Stores (Redis/DynamoDB)   - PACELC Theoretical Model       - WiredTiger Engine       |
|  - Column-Family (Cassandra/HBase)     - ACID vs. BASE Frameworks       - 12-byte ObjectId Spec   |
|  - Graph Databases (Neo4j/Neptune)     - Eventual vs. Strong States     - mongosh & Document Model|
+---------------------------------------------------------------------------------------------------+

This unit establishes the foundational principles of modern non-relational database engineering:

  1. The NoSQL Movement: Origins, philosophy ("Not Only SQL"), and structural taxonomy.
  2. Distributed Systems Foundations: The CAP theorem, PACELC model, and ACID vs. BASE consistency paradigms.
  3. MongoDB Architecture & Fundamentals: The document-oriented paradigm, BSON binary serialization, WiredTiger storage engine internals, and 12-byte ObjectId mechanics.

2. The NoSQL Paradigm: Taxonomy and Theoretical Drivers

2.1 The Concept of "Not Only SQL"

The term NoSQL does not imply an outright rejection of SQL; rather, it denotes "Not Only SQL"—a movement advocating polyglot persistence where the chosen database engine aligns with the specific access patterns, scale, and schema characteristics of the domain.

CharacteristicTraditional Relational Databases (RDBMS)Modern NoSQL Databases
Data ModelNormalized tabular relations (rows and columns)Diverse: Document, Key-Value, Wide-Column, Graph
Schema EnforcementStrict, pre-declared schema (CREATE TABLE)Flexible, dynamic, self-describing schemas
Scalability ModelVertical (Scale-Up: CPU, RAM, NVMe on single box)Horizontal (Scale-Out: Sharding across commodity nodes)
Query MechanismStandard declarative SQL with relational joinsProprietary APIs, JSON query documents, Graph Cypher
Integrity ModelStrict ACID guarantees across multiple tablesTunable consistency: BASE (Eventual) to scoped ACID
Data RelationshipsNormalized Foreign Keys resolved at query runtimeDe-normalized embedded objects or adjacency lists

2.2 The Four NoSQL Database Families

NoSQL engines diverge into four primary architectural families based on how storage blocks organize and index data:

CODE
+---------------------------------------------------------------------------------------------------+
|                                     THE 4 NOSQL ARCHITECTURES                                     |
+---------------------------------+---------------------------------+-------------------------------+
|  DOCUMENT STORES                |  KEY-VALUE STORES               |  WIDE-COLUMN STORES           |
|  - MongoDB, CouchDB, RavenDB    |  - Redis, Memcached, DynamoDB   |  - Apache Cassandra, HBase    |
|  - Semi-structured JSON/BSON    |  - O(1) hash map access         |  - Dynamic sparse tables      |
|  - Deep nested hierarchies      |  - Caching & session stores     |  - High-velocity time-series  |
+---------------------------------+---------------------------------+-------------------------------+
|  GRAPH DATABASES                                                                                  |
|  - Neo4j, Amazon Neptune, TigerGraph                                                              |
|  - Index-free adjacency; Nodes, Edges, Properties; Social networks, fraud detection, routing      |
+---------------------------------------------------------------------------------------------------+

Detailed Family Comparison:

CODE
+----------------------------------------------------------------------------------------------------+
| Model         Data Structure                Strengths                     Typical Workloads        |
+----------------------------------------------------------------------------------------------------+
| Document      BSON / JSON Document Trees    Schema evolution, rich queries Content management,     |
|               with nested arrays/objects    secondary indexing, aggregation e-commerce, catalogs   |
|                                                                                                    |
| Key-Value     Associative array / Hash Map  Ultra-low latency, O(1) ops,  Session management,      |
|               Key -> Opaque Byte Array      massive read/write throughput user profiles, caching   |
|                                                                                                    |
| Wide-Column   Multi-dimensional map         Massive horizontal writes,    Financial transactions,  |
| (Columnar)    RowKey -> Column -> Value     column-family compression     IoT metrics, telemetry   |
|                                                                                                    |
| Graph         Nodes (Entities) & Edges      Complex relation traversal    Social graphs, knowledge |
|               (Relationships with props)    without expensive table joins graphs, fraud networks   |
+----------------------------------------------------------------------------------------------------+

3. Distributed Systems Foundations: CAP, PACELC, and BASE

3.1 The CAP Theorem (Brewer's Theorem)

Formulated by Eric Brewer and formally proven by Seth Gilbert and Nancy Lynch in 2002, the CAP Theorem states that in any asynchronous distributed data store communicating over an untrusted network, the system can guarantee at most two out of three fundamental properties simultaneously:

CODE
                            [ Consistency (C) ]
                                    /\
                                   /  \
                                  /    \
                                 /      \
                                /  CAP   \
                               / TRIANGLE \
                              /            \
                             /              \
    [ Availability (A) ] -------------------- [ Partition Tolerance (P) ]
  1. Consistency (Linearizability / Single-Copy Consistency):
    Every read operation receives the most recent write or an explicit error. All nodes in the cluster observe an identical state at any given point in time.
  2. Availability:
    Every non-failing node returns a non-error response for every received request (without guaranteeing that it contains the most recent write).
  3. Partition Tolerance:
    The system continues to operate despite an arbitrary number of messages being dropped, delayed, or partitioned by the underlying network.

The Distributed Reality: PP is Non-Negotiable

In physical networks, hardware switches fail, optical cables are severed, and packet loss occurs. Therefore, Partition Tolerance (PP) cannot be sacrificed in a distributed network.
When a network partition inevitably occurs, the distributed architect is forced into a binary trade-off:

Distributed Choice during Partition∈{CP,AP}\text{Distributed Choice during Partition} \in \{ \mathbf{CP}, \mathbf{AP} \}

  • CP (Consistency + Partition Tolerance):
    • If a network partition isolates nodes, minority nodes reject read/write requests or wait until consensus is achieved, preserving strict data correctness at the expense of availability.
    • Examples: MongoDB (with primary election consensus), HBase, Apache ZooKeeper.
  • AP (Availability + Partition Tolerance):
    • Both sides of the partitioned network continue accepting reads and writes. Data diverges across partitions, sacrificing immediate consistency to guarantee 100% operational availability. Divergent states are reconciled later.
    • Examples: Apache Cassandra, Amazon DynamoDB, CouchDB.

3.2 The PACELC Theoretical Model

Daniel Abadi recognized that the CAP theorem only addresses system behavior during an active network partition. In normal operations (the vast majority of the time), a different trade-off exists between Latency and Consistency.
The PACELC Theorem formalizes this reality:

If P (Partition)  ⟹  Choose A or C;Else (E)  ⟹  Choose L (Latency) or C (Consistency)\text{If } \mathbf{P} \text{ (Partition)} \implies \text{Choose } \mathbf{A} \text{ or } \mathbf{C}; \quad \text{Else } (\mathbf{E}) \implies \text{Choose } \mathbf{L} \text{ (Latency) or } \mathbf{C} \text{ (Consistency)}

  • PC/EC (e.g., MongoDB): During partitions, chooses Consistency over Availability; in normal operations, chooses Consistency over Latency.
  • PA/EL (e.g., Cassandra, DynamoDB): During partitions, chooses Availability over Consistency; in normal operations, chooses Latency over Consistency (writes return immediately without waiting for cross-node synchronization).

3.3 ACID vs. BASE Consistency Paradigms

CODE
+------------------------------------+------------------------------------+
|  ACID (Relational Core)            |  BASE (Distributed NoSQL Core)    |
|  - Atomicity (All or nothing)      |  - Basically Available (Best-effort|
|  - Consistency (Valid transitions) |    uptime across nodes)            |
|  - Isolation (Transactions isolated|  - Soft state (State may change   |
|  - Durability (Committed to disk)  |    without user input via sync)    |
|  Pessimistic, Synchronous Locking  |  - Eventual consistency (Replicas  |
|                                    |    converge over time t -> inf)    |
|                                    |  Optimistic, Asynchronous Gossip   |
+------------------------------------+------------------------------------+

Eventual Consistency Explained:

In a BASE system, if no new updates are made to a given data item, all replicas across the distributed cluster will eventually synchronize and return the identical value:

lim⁡t→∞P(Replicai(t)=Replicaj(t))=1\lim_{t \to \infty} P(\text{Replica}_i(t) = \text{Replica}_j(t)) = 1


4. MongoDB Architecture and Internal Document Model

4.1 Structural Hierarchy: RDBMS vs. MongoDB

MongoDB organizes data into a clean, hierarchical document structure that mirrors modern application object graphs:

CODE
+-------------------------------------------------------------------------------+
| RDBMS Concept           | MongoDB Equivalent   | Architectural Description    |
+-------------------------+----------------------+------------------------------+
| Database                | Database             | Physical container of collections
| Table                   | Collection           | Grouping of related documents (schema-free)
| Row / Record            | Document             | Polymorphic BSON object record
| Column                  | Field                | Key-value pair within a document
| Index                   | Index                | B-tree / WiredTiger index structures
| Foreign Key / Join      | Embedded Doc / $lookup| Denormalized nesting or aggregation lookup
+-------------------------+----------------------+------------------------------+
CODE
[ Database: 'ecommerce' ]
           |
[ Collection: 'orders' ]
           |
+----------+------------------------------------------------------------+
| Document 1: {                                                         |
|   "_id": ObjectId("67180000a1b2c3d4e5f60001"),                        |
|   "order_id": "ORD-9021",                                             |
|   "customer": { "name": "Alice Smith", "email": "alice@corp.com" },   |
|   "items": [                                                          |
|     { "sku": "SSD-2TB", "qty": 2, "price": 149.99 },                  |
|     { "sku": "RAM-32GB", "qty": 1, "price": 89.50 }                   |
|   ],                                                                  |
|   "payment_status": "PAID"                                            |
| }                                                                     |
+-----------------------------------------------------------------------+

4.2 The BSON (Binary JSON) Serialization Format

While developers interact with MongoDB using JavaScript Object Notation (JSON), MongoDB stores and transmits data internally in BSON (Binary JSON).

Why BSON instead of plain text JSON?

  1. Lightweight & High-Speed Parsing: BSON prefixes string values, arrays, and sub-documents with integer byte-length headers. A traversal algorithm can jump directly over unwanted fields in O(1)O(1) time without scanning individual characters.
  2. Expanded Data Type Support: JSON natively supports only string, number, boolean, array, object, and null. It lacks dedicated types for binary data, dates, and precise numbers. BSON introduces critical enterprise primitives:
    • ObjectId: 12-byte globally unique identifier.
    • Date: 64-bit UTC integer timestamp (milliseconds since Unix epoch).
    • Int32 and Int64: Distinct 32-bit and 64-bit signed integers.
    • Decimal128: IEEE 754-2008 high-precision 128-bit floating-point format for financial calculations (eliminating binary floating-point rounding errors).
    • BinData: Byte array for storing binary payloads (UUIDs, serialized crypto hashes).
    • Regex and Timestamp: Specialized internal types.

4.3 The 12-Byte ObjectId Specification

Every MongoDB document requires an immutable, primary key field named _id. If omitted during document insertion, the MongoDB client driver automatically synthesizes a 12-byte ObjectId.

ObjectId Layout (12 Bytes / 24 Hexadecimal Characters)\text{ObjectId Layout (12 Bytes / 24 Hexadecimal Characters)}

CODE
+-------------------+-----------------------------+-----------------------+
|  4 Bytes (0 - 3)  |      5 Bytes (4 - 8)        |    3 Bytes (9 - 11)   |
+-------------------+-----------------------------+-----------------------+
|  Unix Timestamp   |  Random Unique Value        |  Incrementing Counter |
|  (Seconds since   |  (Host Machine + PID Hash)  |  (Initialized Random, |
|   Epoch in hex)   |                             |   Monotonically Inc.) |
+-------------------+-----------------------------+-----------------------+
  • Bytes 0–3 (4 Bytes): A 32-bit Unix epoch timestamp recording the exact second the document was created.
    • Architectural Benefit: Documents are naturally ordered by insertion time. Administrators can extract creation timestamps directly from the _id field without allocating a separate created_at timestamp:
      javascript
      ObjectId("67180000a1b2c3d4e5f60001").getTimestamp();
      
  • Bytes 4–8 (5 Bytes): A cryptographic random value unique to the specific physical host and process generating the ID.
  • Bytes 9–11 (3 Bytes): An auto-incrementing counter initialized to a random number.

This layout guarantees global uniqueness across distributed sharded clusters without requiring a centralized lock coordinator or database sequence generator.


5. Storage Engine Internals: The WiredTiger Architecture

Since MongoDB 3.2, the default storage engine is WiredTiger, an enterprise-grade transactional storage layer designed for modern multi-core processors and non-volatile memory architectures.

CODE
+-------------------------------------------------------------------------------+
|                         WIREDTIGER STORAGE ENGINE                             |
+-------------------------------------------------------------------------------+
|  CLIENT OPERATIONS: Insert / Update / Delete / Find                           |
+-------------------------------------------------------------------------------+
                                  |
                                  v
+-------------------------------------------------------------------------------+
|  IN-MEMORY BUFFER POOL (WiredTiger Cache)                                     |
|  - Default: 50% of (Physical RAM - 1 GiB)                                     |
|  - Lock-Free Hazard Pointers & Multi-Version Concurrency Control (MVCC)       |
|  - Document-Level Locking (No table or collection locks for standard DML)     |
+-------------------------------------------------------------------------------+
         |                                                       |
         | Every 60s (or 2GB dirty)                              | Every 100ms
         v Checkpoint Commit                                     v
+-------------------------------------+ +---------------------------------------+
| DATA FILES (.wt B-Tree on Disk)     | | WRITE-AHEAD JOURNAL (WiredTigerLog)   |
| - Snappy / Zlib Block Compression   | | - Crash Recovery Redo Log             |
| - In-place data safety              | | - Durability across sudden power loss |
+-------------------------------------+ +---------------------------------------+

5.1 Core WiredTiger Primitives

  1. Document-Level Concurrency Control:
    Unlike legacy MMAPv1 (which enforced database or collection-level locking), WiredTiger implements document-level locking using optimistic Multi-Version Concurrency Control (MVCC). Multiple concurrent threads can write to different documents in the same collection simultaneously without contention.

  2. Transparent Data Compression:
    WiredTiger reduces disk footprint and I/O pressure through block compression:

    • Collections are compressed by default using Snappy (balanced CPU overhead and compression ratio) or Zlib (high compression ratio).
    • Indexes use prefix compression, saving up to 50% of RAM.
  3. The In-Memory Cache:
    By default, WiredTiger reserves:

    Cache Size=0.50×(Physical RAM−1 GiB)\text{Cache Size} = 0.50 \times (\text{Physical RAM} - 1\text{ GiB})

    Dirty pages are modified in cache and flushed periodically to disk.

  4. Checkpoints and Journaling:

    • Checkpoints: Every 60 seconds (or after 2 GB of dirty data), WiredTiger takes an atomic filesystem checkpoint, flushing clean states to .wt disk files.
    • Journaling: To ensure durability between checkpoints, all write operations are written sequentially to an on-disk redo log (the journal) every 100 ms (or on j: true). If a hard power failure occurs, WiredTiger replays the journal from the last checkpoint, guaranteeing zero committed data loss.

6. The MongoDB Developer Toolchain

6.1 The Modern Interactive Shell (mongosh)

The modern MongoDB Shell (mongosh) is a Node.js-based interactive environment supporting modern ECMAScript, syntax highlighting, autocompletion, and rich terminal output.

bash
# Connecting to local MongoDB instance
mongosh "mongodb://localhost:27017"

# Inspecting databases
show dbs

# Switching active database context
use enterprise_db

# Inspecting collections
show collections

6.2 MongoDB Compass (GUI)

MongoDB Compass is the official graphical user interface:

  • Visualizes document schema structures and type distributions.
  • Explains query performance with graphical execution plans (explain()).
  • Provides interactive index management and visual aggregation pipeline builders.

6.3 MongoDB Atlas

MongoDB Atlas is the fully managed multi-cloud database service:

  • Automates sharding, zero-downtime rolling upgrades, and cross-region replica sets across AWS, Azure, and Google Cloud.
  • Built-in automated backups, telemetry alerts, and auto-scaling compute tiers.

7. Comparative Architecture Matrix

DimensionRelational (SQL)Document (MongoDB)Key-Value (Redis)Wide-Column (Cassandra)Graph (Neo4j)
Data PrimitiveTuples in TablesBSON Documents in CollectionsKey -> Value PairsSparse rows with dynamic columnsNodes & Relationships
Schema FlexibilityRigid compile-timeDynamic run-timeSchema-free (opaque)Column-family schemaSchema-flexible
Transaction ModelFull ACIDScoped multi-doc ACIDSingle-key atomicTunable consistencyFull ACID
CAP ClassificationCA (Single node)CP (Default)CP / APAPCA (Single node)
Join SupportNative (O(Nlog⁡N)O(N \log N))$lookup aggregationNoneNoneIndex-free traversal
Horizontal ScaleComplex (Read replicas)Native ShardingCluster partitioningPeer-to-peer ringMaster-replica

8. Summary of Essential Commands

javascript
// mongosh Basic Administration
db.help()                              // Show database methods
db.stats()                             // Return storage and index statistics
db.version()                           // Output MongoDB server version

// Working with Documents
db.users.insertOne({ name: "Bob", role: "admin" }) // Insert single document
db.users.find({ role: "admin" }).pretty()          // Query matching documents
db.users.countDocuments({ role: "admin" })         // Count matching records

// Inspecting System State
rs.status()                            // View replica set status
sh.status()                            // View cluster sharding status
db.serverStatus().wiredTiger           // Query WiredTiger cache telemetry