0% found this document useful (0 votes)
14 views15 pages

LectureNotes NoSQL

The lecture notes cover NoSQL database programming with a focus on MongoDB and its integration with Java. Key topics include data structures, transitioning from SQL to NoSQL, MongoDB architecture, CRUD operations, and the aggregation pipeline. The course aims to equip students with the skills to design document schemas, execute complex queries, and build Java applications that interact with MongoDB.

Uploaded by

umadivine80
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

LectureNotes NoSQL

The lecture notes cover NoSQL database programming with a focus on MongoDB and its integration with Java. Key topics include data structures, transitioning from SQL to NoSQL, MongoDB architecture, CRUD operations, and the aggregation pipeline. The course aims to equip students with the skills to design document schemas, execute complex queries, and build Java applications that interact with MongoDB.

Uploaded by

umadivine80
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture Notes on NoSQL Database Programming

Slide 1: Title Slide

Title: NoSQL Database Programming

Subtitle: NoSQL & MongoDB with Java Integration

Course Code: UPH-CSC-398.1

Session/Semester: 2025/2026- Second Semester

Slide 2: Course Overview & Objectives

Agenda:

• Understanding the structure of data (Structured, Semi-structured,


Unstructured).

• Transitioning from Relational (SQL) to Non-Relational (NoSQL) databases.

• Deep dive into MongoDB: Architecture, CRUD, Aggregation, and


Administration.

• Bridging the gap: Integrating MongoDB with Java applications using the
native driver and ODM (MongooseJS concept adapted for Java).

Learning Outcomes:

• Differentiate between database types based on data structure.

• Design effective document schemas in MongoDB.

• Execute complex queries using the aggregation pipeline.

• Build a Java console application that performs full CRUD operations on


MongoDB.

1
Slide 3: The Structure of Data

Three Main Types:

1. Structured Data:

o Format: Tabular (Rows & Columns). o Schema: Rigid,


predefined (e.g., SQL Tables).

o Example: Financial ledgers, Employee records.

2. Semi-structured Data:

o Format: Self-describing (Tags/Markers separate elements). o


Schema: Flexible ("schema-on-read").

o Example: JSON, XML, HTML.

3. Unstructured Data:

o Format: Raw, no predefined structure. o Example: Images,


videos, text files, streaming logs.

Focus: This course focuses on systems that handle Structured (via conversion) and
Semi-structured data.

Slide 4: The Great Conversion: Relational to JSON

Scenario: You have a traditional RDBMS (e.g., MySQL) and need to move to a
document store.

The Problem (Impedance Mismatch):

• Relational (Normalized): Data is split across tables

(e.g., Orders and OrderDetails). Requires JOIN operations.

• Document (Denormalized): Data is accessed as a single unit. No JOINs in

the traditional sense.

2
Conversion Logic (Pseudocode):

pseudocode

FUNCTION convertSQLtoJSON(userId):

// 1. Query the User table for the main details userData


= SELECT * FROM Users WHERE id = userId

// 2. Query the Orders table (child) for related records


ordersList = SELECT * FROM Orders WHERE user_id = userId

// 3. FOR each order, query the Order_Items (grandchild)


FOR each order IN ordersList:

items = SELECT * FROM Order_Items WHERE order_id = [Link]


[Link] = items // Nest the items inside the order

END FOR

// 4. Nest the entire orders list inside the user document


[Link] = ordersList

// 5. Return the nested structure

RETURN [Link](userData)

END FUNCTION

Slide 5: Unstructured/Streaming Data to BSON

Problem: How do we store log streams or sensor data that never stops?

Solution: BSON (Binary JSON).

• BSON extends JSON by adding support for data types not native to JSON
(e.g., Date, Binary data).

• It is designed for efficiency in space and scan-speed.

3
Example: IoT Sensor Stream

Raw Stream: "sensor:A, temp:22.5, time:1690000000" Converted


to BSON (Conceptual):

javascript

{ sensor_id: "A", // String temperature: 22.5, //


Double (Native Type) timestamp: ISODate("2023-07-
22T10:00:00Z") // Date Object

Slide 6: Introduction to NoSQL

What is NoSQL? "Not Only SQL." A broad class of database management systems
that differ from classic relational systems.

The Four Main Types:

1. Document Stores (MongoDB, CouchDB): Data stored as documents


(JSON/BSON).

2. Key-Value Stores (Redis, DynamoDB): Simple, fast lookup by a unique key.

3. Column-Family Stores (Cassandra, HBase): Data stored in columns rather


than rows, optimized for analytics.

4. Graph Databases (Neo4j): Data stored as nodes and edges, optimized for
relationships.

Why NoSQL?

• Scalability: Horizontal scaling (sharding) is native.

• Flexibility: Schema-less design allows rapid iteration.

• Performance: Optimized for specific data models (e.g., document retrieval).

Slide 7: Focus: Document-Oriented NoSQL MongoDB


is the market leader in Document Stores.

Core Concept:

• Data is stored in BSON documents.

• A Collection is a group of documents (analogous to a Table in SQL).

• Documents in the same collection do not need to have the same structure
(schema-less).

4
Analogy:

• SQL Table: Like a pre-printed form with blank spaces for every field. If you
don't have data for a field, you leave it blank (NULL).

• MongoDB Collection: Like a stack of sticky notes. Each note can have
whatever information is relevant written on it. If it doesn't apply, you don't write
it.

Slide 8: MongoDB Data Format & Structure

The Hierarchy:

1. Database: Physical container for collections.

2. Collection: Logical grouping of documents (e.g., students, products).

3. Document: The actual data record.

Sample Document (students collection):

javascript

_id: ObjectId("507f1f77bcf86cd799439011"), // Unique identifier (added automaticall


y) name: "Alice Smith", major: "Computer Science", enrollment_year: 2023,
courses: [ // Example of nesting (array)

code: "CS101",
grade: "A"

],

graduation_date: null // Explicit null for missing data

5
Slide 9: Grouping Documents & Replica Sets

1. Grouping Documents (Data Modeling):

• Embedding: Putting related data inside a single document (good for one-
tomany relationships where the "many" data is always viewed with the
parent).

• Referencing: Storing the _id of another document (like a foreign key, good

for many-to-many).

2. Replica Sets (High Availability):

• A MongoDB Replica Set is a cluster of MongoDB servers that maintain the


same data set.

• Primary: Receives all write operations.

• Secondaries: Replicate the primary's data (read-only). If the primary fails, an


election occurs and a secondary becomes the new primary.

• Benefit: Automatic failover and data redundancy.

Slide 10: MongoDB CRUD Operations

The four basic functions of persistent storage.

Operation MongoDB Command (Shell) SQL Equivalent

Create [Link]({...}) INSERT INTO table ...

Read [Link]({...})
SELECT * FROM table WHERE ...

Update
[Link]({...}, UPDATE table SET ... WHERE
{$set: {...}}) ...

Delete [Link]({...})
DELETE FROM table WHERE
...

6
Key Difference:

• find() returns a cursor, not the actual data immediately (allows iteration).

• Update operations often use modifiers like $set, $push, $inc.

Slide 11: MongoDB Shell & Compass

1. MongoDB Shell (mongosh):

• The interactive JavaScript interface to MongoDB.

• Used for administration and quick queries.

Example:

javascript use university_db [Link]({ major:


"Computer Science" }).pretty()

2. MongoDB Compass:

• The Graphical User Interface (GUI).

• Features:

o Visualize and explore data. o Build queries with


a visual query builder.

o View and optimize query performance (Explain


Plans). o Manage indexes and schema validation.

Slide 12: Importing and Exporting Data

Moving data in and out of the database.

Tools: mongoimport and mongoexport (JSON/CSV).

Scenario: Populating the students collection from a JSON file.

Step 1: Create [Link]

json

{ "name": "Bob Jones", "major": "Physics", "enrollment_year": 2022 } {


"name": "Carol Danvers", "major": "Aeronautics", "enrollment_year": 2021 }

Note: This is JSON Lines format (one valid JSON object per line).

7
Step 2: Run the command (Terminal)

bash mongoimport --db university_db --collection students --file [Link] --


jsonArray

(Use --jsonArray if your file is a single array [ {...}, {...} ])

Slide 13: The Aggregation Pipeline

A framework for data aggregation modeled on the concept of data processing


pipelines. The document enters the pipeline and is transformed through stages.

Syntax: [Link]( [ {stage1}, {stage2}, ... ] )

Common Stages:

• $match: Filters the documents (like find).

• $group: Groups documents by a specified key (like GROUP BY).

• $sort: Sorts the documents.

• $project: Reshapes the document (select/include/exclude fields).

Use Case: Find the average enrollment year by major.

javascript [Link]([

{ $group: { _id: "$major", averageYear: { $avg: "$enrollment_year" } } },


{ $sort: { averageYear: -1 } }

])

Slide 14: Java Integration with MongoDB

We will use the official MongoDB Java Driver (Synchronous).

Setup (Maven [Link]):

xml

<dependency>

<groupId>[Link]</groupId>

<artifactId>mongodb-driver-sync</artifactId>

<version>4.11.0</version> // Use latest stable


</dependency>

8
Connection Logic:

java import [Link];


import [Link]; import
[Link]; import
[Link]; import
[Link]; // BSON Document class

public class MongoDBConnection {

public static void main(String[] args) {

// 1. Connect to server (default: localhost:27017)

MongoClient mongoClient = [Link]("mongodb://localhost:27017")

// 2. Get database (creates if doesn't exist)

MongoDatabase database = [Link]("university_db");

// 3. Get collection (creates if doesn't exist)

MongoCollection<Document> collection = [Link]("students");

[Link]("Connected successfully!");

// Close connection when done (usually in finally block)


[Link]();

9
Slide 15: Java CRUD - Create (Insert)

Logic: Convert Java data types to BSON Document objects and insert.

public static void insertStudent(MongoCollection<Document> collection) {

// Create a new document (BSON)

Document student = new Document("name", "David Miller")

.append("major", "Computer Engineering")

.append("enrollment_year", 2024)
.append("courses", [Link]( new Document("code",
"CE201").append("grade", "B+")

));

// Insert the document


[Link](student);

[Link]("Student inserted. ID: " + [Link]("_id")); }

Slide 16: Java CRUD - Read (Query)

Logic: Use Filters helpers to create query conditions.

Programming Example:

import static [Link].*;

public static void findCSStudents(MongoCollection<Document> collection) {

// Find all students where major equals "Computer Science"

// and enrollment year is greater than 2020

FindIterable<Document> iterable = [Link](


and(eq("major", "Computer Science"), gt("enrollment_year", 2020))

);

// Iterate over the results for


(Document doc : iterable) {

[Link]([Link]());

10
Slide 17: Java CRUD - Update

Logic: Use Updates helpers to define modification operations.

Programming Example:

java import static


[Link].*; import static
[Link];

public static void updateStudentGrade(MongoCollection<Document> collection) {

// Update: For student named "Alice Smith", set the grade of CS101 to "A+"

// This is complex because "courses" is an array of nested docs.

// We use the positional operator '$' in the update. Bson filter = and(
eq("name", "Alice Smith"), eq("[Link]", "CS101") // Find the document
with the matching array elem ent

);

Bson update = set("courses.$.grade", "A+"); // '$' refers to the matched array index

UpdateResult result = [Link](filter, update);

[Link]("Modified count: " + [Link]());

Slide 18: Java CRUD - Delete Logic:


Simple delete based on a filter.

Programming Example:

java import static


[Link];

public static void deleteLowPerformer(MongoCollection<Document> collection) {

11
// Delete a student who dropped out (e.g., name "David Miller")
Bson filter = eq("name", "David Miller");

DeleteResult result = [Link](filter);

[Link]("Deleted count: " + [Link]());

Slide 19: Indexing and Query Optimization

Why Indexes? Without indexes, MongoDB must scan every document in a collection
(collection scan) to find matching documents.

Creating an Index:

javascript

// Shell: Create an ascending index on the 'name' field


[Link]( { name: 1 } )

Query Analysis:

javascript

// Shell: Explain how a query is executed [Link]( { name:


"Alice Smith" } ).explain("executionStats")

• Look for "stage": "COLLSCAN" (bad) vs. "stage":


"FETCH" with "indexName" (good).

Projection: Returning only specific fields to save network bandwidth.

javascript

// Get ONLY the name and major (excluding _id)

[Link](

{ major: "Physics" },

{ name: 1, major: 1, _id: 0 }

12
Slide 20: Case Study: E-Commerce Order System

Scenario: An online store needs to manage Users, Products, and Orders.

SQL Design (Normalized):

Tables: Users, Products, Orders, OrderItems (Join nightmare).

MongoDB Design (Hybrid Embedding):

• Collection: users (Contains user profile).

• Collection: products (Contains catalog info).

• Collection: orders (Embeds the order items and product snapshot).

Sample order Document:

javascript

_id: ObjectId("..."), user_id: ObjectId("..."), //


Reference to the user order_date:
ISODate("..."), total: 1250.00, items: [

product_id: ObjectId("..."), // Reference name: "Gaming Laptop", //


Snapshot (in case product name changes later) quantity: 1, price: 1200.00

},

product_id: ObjectId("..."),
name: "Mouse Pad",
quantity: 2, price: 25.00

13
Slide 21: Practical: Running the Demo

Setup:

1. Start MongoDB: mongod --dbpath /path/to/data 2.


Run Java program to insert 10 sample students.

3. Run Aggregation query via Java to find the top 3 majors with the highest
average enrollment year.

Sample Dataset (students collection after insert):

_id name major enrollment_year

ObjectId(...) Alice Smith Computer Science 2023

ObjectId(...) Bob Jones Physics 2022

ObjectId(...) Carol Danvers Aeronautics 2021

ObjectId(...) Diana Prince History 2024

ObjectId(...) Eve Adams Computer Science 2024

Test:

• Query: Find all students enrolled in 2024.

• Expected Result: Carol Danvers? (No, she's 2021). Diana Prince, Eve
Adams.

14
Slide 22: Summary & Further Reading

Key Takeaways:

• NoSQL, specifically MongoDB, provides flexibility for modern, rapidly


changing applications.

• The document model (BSON) maps naturally to objects in programming


languages (like Java POJOs).

• Aggregation pipelines are a powerful alternative to SQL GROUP BY for


analytics.

• Java integration is straightforward using the official driver's fluent API.

Further Exploration:

• MongooseJS: If you move to [Link], Mongoose provides a schema-based


solution for MongoDB, offering data validation and type casting.

• Spring Data MongoDB: Integrates MongoDB with the Spring framework for
enterprise Java applications.

• Atlas: MongoDB's fully-managed cloud database service.

15

You might also like