Unit 5
Unit 5
Individuals
Businesses
Categories of Data
Data can be categorized as either
structured or unstructured data
Over 80% of enterprise
information is unstructured E-Mail Attachments PDFs
X-Rays
Check Unstructured (80%)
Images
Documents
Forms
Web Pages
Contracts
Rich Media
Invoices
Audio Video
Structured (20%)
Rows and Columns
Introduction
Enormous data that comes out every day from the web
Handled by Relational database management systems
(RDBMS).
The relational model is well-suited to client-server
programming and today it is predominant technology for
storing structured data in web and business applications.
Classical relation database follow the ACID Rules
Atomic : A transaction is a logical unit of work which must
be either completed with all of its data modifications, or
none of them is performed.
Consistent : At the end of the transaction, all data must be
left in a consistent state.
Isolated : Modifications of data performed by a transaction
must be independent of another transaction. Unless this
happens, the outcome of a transaction may be erroneous.
Durable : When the transaction is completed, effects of the
modifications performed by the transaction must be
permanent in the system.
Distributed Systems
• A distributed system consists of multiple computers and
software components that communicate through a
computer network (a local network or by a wide area
network).
• A distributed system can consist of any number of possible
configurations, such as mainframes, workstations, personal
computers, and so on.
• The computers interact with each other and share the
resources of the system to achieve a common goal.
Advantages of Distributed Computing
Reliability (fault tolerance) : The important advantage of distributed
computing system is reliability. If some of the machines within the system
crash, the rest of the computers remain unaffected and work does not stop.
Scalability : In distributed computing the system can easily be expanded by
adding more machines as needed.
Sharing of Resources : Shared data is essential to many applications such as
banking, reservation system. As data or resources are shared in distributed
system, other resources can be also shared (e.g. expensive printers).
Flexibility: As the system is very flexible, it is very easy to install, implement
and debug new services.
Speed: A distributed computing system can have more computing power and
it's speed makes it different than other systems.
Open system :As it is open system, every service is equally accessible to
every client i.e. local or remote.
Performance: The collection of processors in the system can provide higher
performance (and better price/performance ratio) than a centralized
computer.
Disadvantages of Distributed Computing
Troubleshooting :
Troubleshooting and diagnosing problems.
Software :
Less software support is the main disadvantage of
distributed computing system.
Networking :
The network infrastructure can create several
problems such as transmission problem,
overloading, loss of messages.
Security :
Easy access in distributed computing system
increases the risk of security and sharing of data
generates the problem of data security
What is NoSQL ?
NoSQL is a non-relational database management
systems, different from traditional relational database
management systems in some significant ways.
• It is designed for distributed data stores where very
large scale of data storing needs (for example Google
or Facebook which collects terabits of data every day
for their users).
• These type of data storing may not require fixed
schema, avoid join operations and typically scale
horizontally.
Why NoSQL ?
In today’s time data is becoming easier to access and
capture through third parties such as Facebook,
Google+ and others. Personal user information,
social graphs, geo location data, user-generated
content and machine logging data are just a few
examples where the data has been increasing
exponentially. To avail the above service properly,
it is required to process huge amount of data.
Which SQL databases were never designed. The
evolution of NoSql databases is to handle these
huge data properly.
Why NoSQL ?
NoSQL Products/Projects
[Link] 122
NoSQL Databases
Cassandra
CouchDB
MongoDB
StupidDB
Etc.
18 4/3/2026
RDBMS vs NoSQL
RDBMS NoSQL (Not Only SQL )
Durable
Four categories of NOSQL
Databases/Data Models
Key-Value Stores
Document Stores
Wide Column Stores
Graph Databases
Key-Value Stores
Key valued stores are those types of NoSQL database that are scheme
free, and also your values stored as key i.e in one column you will be
having a key “Name” and the value would be “Zack” and in the
second column it’s not necessary mean that you must have the
value of Name again you could store different kind of data in the
same column in different row, and also you could have more column
in one row than previous or vice versa, this is the most common kinds
of NoSQL database that are currently in the market and other kinds of
NoSQL database are built upon the principle of this kinds of NoSQL
database and added some features on that.
-Riak
-redis
-Azure table storage
-Amazon SimpleDB
-Dynomite
-BerkeleyDB
Document Stores
These kinds of NoSQL database are very interesting because instead of
rows and columns your data is stored in documents and these
simply sored in JavaScript like JASON JavaScript Object
Notation and the language of the database that is used internally is
JavaScript.
Since JavaScript is very popular in the web market and most of the
developer are familiar with that language already these kinds of
NoSQL database are very popular with web developer and also these
kinds of NoSQL database are same as Key-Value Stores are Scheme
free.
CouchDB
-MongoDB
-TeraStore
-MarkLogic
- Jackrabbit
Wide Column Stores/Extensible Record Stores
This is another kinds of NoSQL database which they are not Scheme free
they are kinds of semi Structured database which means you need to
specify a groups of columns in these database and they are called
Column Families what it really means is that you will have some
kinds of master Column name and then in each row you could
have different column names which would be different from your
master column name.
-Apache Hbase
- Cassandra
- BigTable
- Hypertable
- Qbase
- OpenNeptune
- Sqrrl Enterprise
Graph Databases
These kinds of NoSQL database are the most specialized kinds of
database they are focused on the relationship between entities rather
than entities themselves, they are mostly famous on social media and
very useful in that field because it’s best to use in social graphs and
taking your social media and your group of friends you could
better understand these kinds of database.
-NeoJ4
-Sones
-InfiniteGraph
-OrientDB
-AllegroGraph
-FlockDB
Categories of NoSQL database
Category Description Name of the database
database database
table Collection
Row Document 0r BSON Document
Column Field
Index Index
Table Join Embedded documents & Linking
Insert Command:
> db.Teacher_info.find()
Output:
{ "_id" : ObjectId("53a2d8ac8404f005f1acc666"), "Teacher_id" : "pic001",
"Teacher_name" : "Ravi", "Dept_name" : "IT", "sal" : 20000, "status" : "A"
}
{ "_id" : ObjectId("53a2d8fc8404f005f1acc667"), "Teacher_id" : "pic001",
"Teacher_name" : "Ravi", "Dept_name" : "IT", "sal" : 20000, "status" : "A"
}
{ "_id" : ObjectId("53a2d91b8404f005f1acc668"), "Teacher_id" : "pic003",
"Teacher_name" : "Akshay", "Dept_name" : "IT", "sal" : 25000, "status" :
"N" }
{ "_id" : ObjectId("53a2da038404f005f1acc669"), "Teacher_id" : "pic003",
"Teacher_name" : "Akshay", "Dept_name" : "IT", "sal" : 25000, "status" :
"N" }
>
SQL & Mongodb Commands
SQL SELECT Statements MongoDB find() Statements
Delete Records
DELETE FROM Teacher_info WHERE db.Teacher_info.remove({Teacher_id:
Teacher_id = “pic001" "pic001"});
MySql:
DROP TABLE Teacher_info
Mongo:
db.Teacher_info.drop()
1. MongoDB (Basic Commands)
Stores data in JSON-like documents (BSON)
No fixed schema (flexible structure)
Uses:
o Database → Collection → Document
Example:
{
"name": "Sushma",
"age": 25,
"department": "Computer"
}
2. Start MongoDB
After installation:
Mongosh
4. Collection Commands
Create collection
[Link]("students")
Show collections
show collections
5. Insert Data
Insert one document
[Link]({
name: "Sushma",
age: 22,
course: "MCA"
})
Pretty format
[Link]().pretty()
Find one
[Link]({ name: "Sushma" })
7. Update Data
Update one document
[Link](
{ name: "Sushma" },
{ $set: { age: 23 } }
)
Update many
[Link](
{ course: "MCA" },
{ $set: { status: "active" } }
)
8. Delete Data
Delete one
[Link]({ name: "Amit" })
Delete many
[Link]({ age: { $lt: 22 } })
9. Useful Operators
Comparison
{ age: { $gt: 20 } } // greater than
{ age: { $lt: 25 } } // less than
Logical
{ $and: [ { age: 22 }, { course: "MCA" } ] }
{ $or: [ { age: 22 }, { age: 23 } ] }
Drop database
[Link]()
[Link]([
{ name: "Sushma", age: 22 },
{ name: "Rahul", age: 24 }
])
[Link]()
[Link](
{ name: "Rahul" },
{ $set: { age: 25 } }
)
Logical Operators
Operator Meaning Example
All conditions must { $and: [ { age: 22 }, {
$and
be true course: "MCA" } ] }
{ $or: [ { age: 22 }, { age:
$or Any condition true
23 } ] }
$not Negates condition { age: { $not: { $gt: 22 } } }
{ $nor: [ { age: 22 }, {
$nor All conditions false
course: "MCA" } ] }
Combined Example
Query:
Find students enrolled for MCA course and age is greater than 20.
[Link]({
$and: [
{ age: { $gt: 20 } },
{ course: "MCA" }
]
})
Aggregation Pipeline examples in MongoDB with outputs.
Sample Data
[Link]([
{ name: "Sushma", age: 22, marks: 85, course: "MCA" },
{ name: "Rahul", age: 24, marks: 78, course: "MCA" },
{ name: "Neha", age: 21, marks: 92, course: "BCA" },
{ name: "Amit", age: 23, marks: 60, course: "BCA" }
])
1. $match (Filter)
� Query:
[Link]([
{ $match: { marks: { $gt: 80 } } }
])
� Output:
[
{ "name": "Sushma", "age": 22, "marks": 85, "course": "MCA" },
{ "name": "Neha", "age": 21, "marks": 92, "course": "BCA" }
]
� Output:
[
{ "name": "Sushma", "marks": 85 },
{ "name": "Rahul", "marks": 78 },
{ "name": "Neha", "marks": 92 },
{ "name": "Amit", "marks": 60 }
]
� Output:
[
{ "_id": "MCA", "avgMarks": 81.5 },
{ "_id": "BCA", "avgMarks": 76 }
]
� Output:
[
{ "name": "Neha", "marks": 92 },
{ "name": "Sushma", "marks": 85 },
{ "name": "Rahul", "marks": 78 },
{ "name": "Amit", "marks": 60 }
]
5. $limit
� Query:
[Link]([
{ $sort: { marks: -1 } },
{ $limit: 2 }
])
� Output:
[
{ "name": "Neha", "marks": 92 },
{ "name": "Sushma", "marks": 85 }
]
6. $count
� Query:
[Link]([
{ $count: "totalStudents" }
])
� Output:
[
{ "totalStudents": 4 }
]
� Output:
[
{ "_id": "MCA", "totalMarks": 163 },
{ "_id": "BCA", "totalMarks": 152 }
]
� Output:
[
{ "_id": null, "maxMarks": 92, "minMarks": 60 }
]
9. Combined Pipeline �
� Query:
[Link]([
{ $match: { course: "MCA" } },
{
$group: {
_id: "$course",
avgMarks: { $avg: "$marks" }
}
}
])
� Output:
[
{ "_id": "MCA", "avgMarks": 81.5 }
]
� Output:
[
{ "_id": "BCA", "topStudent": "Neha", "highestMarks": 92 },
{ "_id": "MCA", "topStudent": "Sushma", "highestMarks": 85 }
]
MapReduce in MongoDB
Used for processing large data
Works in 3 steps:
1. Map → Emits key-value pairs
2. Reduce → Aggregates values
3. Result → Final output collection
Concept Meaning
emit(key, value) Sends data from map
Map Function Processes each document
Reduce Function Aggregates values
Finalize Function Optional post-processing
out Output collection name
Sample Data
[Link]([
{ name: "Sushma", course: "MCA", marks: 85 },
{ name: "Rahul", course: "MCA", marks: 78 },
{ name: "Neha", course: "BCA", marks: 92 },
{ name: "Amit", course: "BCA", marks: 60 }
])
1. Total Marks per Course
Map Function:
Reduce Function:
var reduceFunction = function(key, values) {
return [Link](values);
};
Query:
[Link](
mapFunction,
reduceFunction,
{ out: "total_marks" }
)
[
{ "_id": "MCA", "value": 163 },
{ "_id": "BCA", "value": 152 }
]
2. Count Students per Course
Map:
Reduce:
Query:
[Link](
mapFunction,
reduceFunction,
{ out: "student_count" }
)
Output:
[
{ "_id": "MCA", "value": 2 },
{ "_id": "BCA", "value": 2 }
]
3. Average Marks per Course
Map:
Reduce:
Query:
[Link](
mapFunction,
reduceFunction,
{
out: "avg_marks",
finalize: finalizeFunction
}
)
Output:
[
{ "_id": "MCA", "value": 81.5 },
{ "_id": "BCA", "value": 76 }
]
4. Maximum Marks per Course
Map:
Query:
[Link](
mapFunction,
reduceFunction,
{ out: "max_marks" }
)
Output:
[
{ "_id": "MCA", "value": 85 },
{ "_id": "BCA", "value": 92 }
]
Notes
Subject: Database Management Systems Lab
[Link] M
Indexing
[Link] M
Indexing
[Link] M
Indexing
Example-
db.Teacher_Info.ensureIndex({“Teacher_id”:1})
[Link] M
Aggregation
[Link] M
Aggregation
Syntax:
Db.COLLECTION_NAME.aggregate(AGGREGAT
E_OPERATION)
Example:
Db.Teacher_Info.aggregate([{$group:{_id:”$DeptN
ame”, Total_sal : {$sum: “$sal”}}}])
[Link] M
Aggregation Expressions
•SUM
•AVG
•MIN
•MAX
•FIRST
•LAST
[Link] M
Pipeline
•Pipeline - output of one operation is given input
to another operation.
•Mongodb uses concept of pipeline in
aggregation framework.
•There can be set of possible stages. At each stage
set of documents are taken as input and produce a
resulting set of documents. The resulted set of
documents then in turn again be used for next
stage and so on.
[Link] M
Stages in aggregation framework
[Link] M
Map Reduce
• A way to divide large query computations into
smaller chunks.
•May run in multiple processes on multiple
machines.
•Used for processing large data sets.
•MapReduce() command is used to peform
mapreduce operation.
[Link] M
•MapReduce Example
•RED 1
•BLUE 2
•gReen 5
•Red 11 •BLUE 14
•Yellow 2 •GREEN 14
•ReD 17 •RED 42
•bLue 12 •YELLOW 12
•GREEN 9
•Red 13
•Input •Output
•Copyright © 2014 IBM
Corporation
•Map
•RED 1 •RED 1
•BLUE 2 •BLUE 2
•gReen 5 •GREEN 5
•Red 11 •RED 11
•Yellow 2 •YELLOW 2
•ReD 17 •RED 17
•bLue 12 •BLUE 12
•GREEN 9 •MAP •GREEN 9
•Red 13 •RED 13
•RED 1 •BLUE 2
•BLUE 2 •BLUE 12
•GREEN 5 •GREEN 5
•RED 11 •GREEN 9
•YELLOW 2 •RED 1
•RED 17 •RED 11
•BLUE 12 •SORT •RED 17
•GREEN 9 •RED 13
•RED 13 •YELLOW 2
•BLUE 2 •BLUE
•BLUE 2
•BLUE 12 •BLUE
•BLUE 12
•GREEN 5
•GREEN 5
•GREEN 9 •GREEN 9
•RED 1
•RED 11 •RED 1
•PARTITION
•RED 11
•RED 17 •RED 17
•RED 13 •RED 13
•YELLOW 2
•YELLOW
•YELLOW 2
•BLUE 2
•BLUE 12
•GREEN 5
•GREEN 9 •BLUE 14
•GREEN 14
•RED 1
•RED
•RED 42
11 •REDUCE •YELLOW 2
•RED 17
•RED 13
•YELLOW 2
•BLUE
•BLUE •2 •YELLOW
•YELLOW •2 • GREEN •9
•YELLOW •2
•YELLOW •2
>[Link](
[Link] M
Map Reduce
The map-reduce function first queries the collection, then maps the result
documents to emit key-value pairs which is then reduced based on the keys
that have multiple values.
map is a java script function that maps a value with a key and emits a key-
value pair
reduce is a java script function that reduces or groups all the documents
having the same key