3 RK_NoSQL_MongoDB_V5
3 RK_NoSQL_MongoDB_V5
No SQL Databases
By
Dr. Tene Ramakrishnudu
Associate Professor
Department of Computer Science &Engineering
National Institute of Technology(NIT), Warangal, TS, India
Outline
❖NoSQL Database
❖Types of NoSQL databases
❖CAP Theorem
❖MongoDB
25-09-2024 RK-CSE-NITW 2
NoSQL Databases
❖Non-relational
❖Open source
❖Distributed Database
25-09-2024 RK-CSE-NITW 3
NoSQL Databases
25-09-2024 RK-CSE-NITW 4
Advantages of NoSQL
❖ Consistency :
▪ all replicas contain the same version of data
▪ a data item behaves as if there is one copy
❖ Availability:
▪ system remains operational on failing nodes
▪ Node failures do not prevent survivors from continuing to operate
❖ Partition tolarence:
▪ multiple entry points system remains operational on system split
▪ The system continues to operate despite network partitions
A P
25-09-2024 RK-CSE-NITW 6
MongoDB
❖NoSQL
❖Non relational
❖Document-oriented database
❖Cross-platform
❖Open Source
❖Distributed
25-09-2024 RK-CSE-NITW 7
MongoDB
25-09-2024 RK-CSE-NITW 8
MongoDB: Replication
Client
Application
Write
Primary
25-09-2024 RK-CSE-NITW 11
MongoDB: Sharding
Collection 1
1 TB Database
Logical Database(collection 1)
❖Document:
25-09-2024 RK-CSE-NITW 14
MongoDB
❖Example:
{
RollNo:123456
Name: abcdef
ContactNo: 0987654321
Email: [email protected]
25-09-2024 RK-CSE-NITW 15
MongoDB
❖Collection:
❖a table in RDBMS.
❖ Example:
db.students.insert({
RollNo:123
Name: abc
ContactNo: 0987654321
Email: [email protected], [email protected]
}
{
RollNo:456
Name: def
ContactNo: 0987654321, 0987654321, 0987654321
Email: [email protected], [email protected], [email protected]
}
{
RollNo:789
Name: ghi
ContactNo: 0987654321, 0987654321
Email: [email protected]
})
25-09-2024 RK-CSE-NITW 17
MongoDB
❖Database:
❖Collection of collections.
❖A database in RDBMS
❖Each database gets its own set of files on the file system
25-09-2024 RK-CSE-NITW 18
MongoDB
25-09-2024 RK-CSE-NITW 19
MongoDB
25-09-2024 RK-CSE-NITW 20
MongoDB
25-09-2024 RK-CSE-NITW 21
MongoDB: Installation
25-09-2024 RK-CSE-NITW 22
MongoDB: Installation
❖8. copy the file path C:\Program Files\ MongoDB\ Server\ 4.4\ bin
❖ 11. if any errors about the data folder go to C drive and create
data folder and inside data folder create db. or else go to step 12.
25-09-2024 RK-CSE-NITW 23
MongoDB: Installation
25-09-2024 RK-CSE-NITW 24
MongoDB: Installation (extra)
❖To run the mongod & mongo any where do the environment
setup
❖2. right click on the windows key then select the system
25-09-2024 RK-CSE-NITW 26
MongoDB
❖CRUD Operations
❖Example:
> use wsdc
switched to db wsdc
25-09-2024 RK-CSE-NITW 27
MongoDB
25-09-2024 RK-CSE-NITW 29
MongoDB
❖Options is optional
❖Fields in theoptions.
❖(i) Capped: Boolean (Optional)
❖If true, enables a capped collection.
❖Capped collection is a fixed size collection that automatically
overwrites its oldest entries when it reaches its maximum size.
❖If you specify true, you need to specify size parameter also.
25-09-2024 RK-CSE-NITW 30
MongoDB
25-09-2024 RK-CSE-NITW 32
MongoDB
>db.students.insert({
rollNo:123
name: “abc”
contactno: 0987654321
email: [email protected]
age: 25
})
25-09-2024 RK-CSE-NITW 33
MongoDB
25-09-2024 RK-CSE-NITW 34
MongoDB
❖Read :
o db. students.find( { age: { $gt: 18 } }, { name: 1, address: 1 } ).limit(5)
❖To specify the greater than condition, query criteria uses the
greater than (i.e. $gt) query selection operator.
❖ The matching documents will return with only the _id, name
and address fields.
25-09-2024 RK-CSE-NITW 35
MongoDB
25-09-2024 RK-CSE-NITW 36
MongoDB
>db. students.updateOne(
{ age: { $lt: 18 } },
{ $set: {contactno: 0987654300} } )
>db. students.updateMany(
{ age: { $gt: 18 } },
{ $set: { status: “accept" } } )
>db. students.replaceOne(
{ name: “abc" },
{ name: “baksdj", age : 25} )
25-09-2024 RK-CSE-NITW 37
MongoDB
25-09-2024 RK-CSE-NITW 38
MongoDB
25-09-2024 RK-CSE-NITW 39
MongoDB
❖Relational Operations:
❖$eq = equal to
db.students.find({gade:{$eq:’Good’}}).pretty()
❖$ne= not equal to
db.students.find({gade:{$ne:’Good’}}).pretty()
❖$gte= greater than or equal to
db.students.find({gade:{$gte:50}}).pretty()
❖$lte= less than or equal to
db.students.find({gade:{$lte:50}}).pretty()
❖$gt= greater than
db.students.find({gade:{$gt:50}}).pretty()
❖$lt= less than
db.students.find({gade:{$lt:50}}).pretty()
25-09-2024 RK-CSE-NITW 40
MongoDB
❖Name contains e
db.students.find({grade:/e/}).pretty()
db.students.find({grade:/.*e.*/}).pretty()
db.students.find({grade:{$regex:”e”}}).pretty()
❖Null values
db.students.find({age:{$eq:”null”}}).pretty()
25-09-2024 RK-CSE-NITW 42
MongoDB
25-09-2024 RK-CSE-NITW 43
MongoDB
❖arrays:
❖Create collection and each document should have a fruits array.
db.food.insert({_id:1,fruits:[“banana”,”apple”,”chery”]})
db.food.insert({_id:2,fruits:[“orange”,”mango”,”chery”})
db.food.insert({_id:3,fruits:[“gapes”,”apple”,”mango”]})
db.food.insert({_id:4,fruits:[“banana”,”apple”,”grape”]})
db.food.insert({_id:5,fruits:[“banana”,”mango”,”chery”]})
25-09-2024 RK-CSE-NITW 44
Design MongoDB for the social E-commerce website
25-09-2024 RK-CSE-NITW 45
MongoDB GridFS
25-09-2024 RK-CSE-NITW 46
Working with GridFS
❖It is a kind of file system to store files but its data is stored
within the MongoDB collections.
25-09-2024 RK-CSE-NITW 48
Working with GridFS
25-09-2024 RK-CSE-NITW 49
Working with GridFS
❖files.filename:
▪ A human-readable name for the GridFS file.
▪ It is optional.
❖files.contentType
▪ A valid MIME type for the GridFS file.
▪ It is optional.
❖files.aliases
▪ An array of alias strings.
▪ It is optional.
❖files.metadata
▪ the additional information
▪ metadata filed can be of any data.
▪ It is optional.
25-09-2024 RK-CSE-NITW 50
Working with GridFS
25-09-2024 RK-CSE-NITW 51
Working with GridFS
❖Step1: Goto https://www.mongodb.com/try/download/database-
tools?tck=docs_databasetools
❖Step 2: Select the Version, Platform and Package then Download the
MongoDB Database tools.
25-09-2024 RK-CSE-NITW 52
25-09-2024 RK-CSE-NITW 53
25-09-2024 RK-CSE-NITW 54
25-09-2024 RK-CSE-NITW 55
25-09-2024 RK-CSE-NITW 56
25-09-2024 RK-CSE-NITW 57
Working with GridFS
❖Step 4: keep the files in the folder where the tools are running
>
❖Step 5: upload files on to the MongoDB using the following
command
goto database sever
C:\Program Files\MongoDB\Tools\100\bin
and execute the following command
>mongofiles.exe -d gridfs put VedaDance.mp3
25-09-2024 RK-CSE-NITW 58
Working with GridFS
>show collections
>db.fs.files.find()
>db.fs.files.find().pretty()
>db.fs.chunks.find({},{data:0})
>db.fs.chunks.find({},{data:0}) .pretty()
25-09-2024 RK-CSE-NITW 59
Working with GridFS
25-09-2024 RK-CSE-NITW 61
Working with GridFS
25-09-2024 RK-CSE-NITW 62
?
25-09-2024 RK-CSE-NITW 63
Refeences
25-09-2024 RK-CSE-NITW 64
Thank You
25-09-2024 RK-CSE-NITW 65