2. 2
50177004-Big Data Analytics-Thinnaphat Borirak
2. Key-Value Database
ในหมวดหมู่ฐานข้อมูล NoSQL การจัดเก็บ Key-Value เป็นวิธีทีง่ายทีสุด ฐานข้อมูลเหล่านีจัดเก็บข้อมูลใน
ลักษณะทีไม่มีแบบแผน โดย Key จะสามารถชีไปทีข้อมูลประเภทใดก็ได้ เช่น Object, String หรือข้อมูลประเภทอืนๆ
ข้อได้เปรียบหลักของฐานข้อมูลเหล่านีคือง่ายต่อการติดตังและเพิมข้อมูลเข้าไป สามารถดึงข้อมูลจากฐานข้อมูลเหล่านี
ได้ด้วยจากการค้นหาของค่าคีย์ แต่ข้อเสียเปรียบหลักคือเราไม่สามารถหาองค์ประกอบใดๆ ตามค่าทีเก็บไว้ได้ ซึงเรา
จําเป็นทีจะต้องค้นหาข้อมูลตามชือของคีย์เสมอ
3. Column Store Database
ฐานข้อมูลประเภทนีเก็บข้อมูลในคอลัมน์ภายในคีย์สเปซ (Key Space) คีย์สเปซ ถูกกําหนดด้วย Name,
Value and Timestamp ทีไม่ซํากันเสมอ ซึงค่อนข้างคล้ายกับฐานข้อมูลในแบบที 2
4. Graph Store Database
ฐานข้อมูลประเภทนีได้รับการออกแบบมาสําหรับข้อมูลทีสามารถแสดงเป็นข้อมูลกราฟได้ง่าย ซึงหมายความ
ว่าข้อมูลจะเชือมโยงกับความสัมพันธ์ของข้อมูลจํานวนไม่แน่นอน เช่น ความสัมพันธ์ในครอบครัวและสังคม เป็นต้น
MongoDB Overview
MongoDB เป็นฐานข้อมูลเชิงเอกสาร (Document Store) ข้ามแพลตฟอร์มทีให้ประสิทธิภาพสูง ความพร้อมใช้งานสูง
และปรับขนาดได้ง่าย MongoDB works on concept of collection and document.
ภาพแสดงการเปรียบเทียบของโครงสร้างฐานข้อมูล RDBMS กับ MongoDB
- Database เป็นส่วนทีใช้เก็บ Collection หรือชุดข้อมูล
- Collection หรือชุดข้อมูลเทียบได้กับตารางข้อมูลเชิงสัมพันธ์
- Document เอกสารทีจัดเก็บข้อมูลของคู่คีย์ (Key) และค่า (Value)
ตารางแสดงความสัมพันธ์ของคําศัพท์ RDBMS กับ MongoDB
RDBMS MongoDB
Database Database
Table Collection
Tuple / Row Document or BSON Document
Column Field
Table Join Embedded Documents and Linking
Primary Key Primary Key (Default key _id provided by MongoDB itself)
Database Server and Client
Mysqld / Oracle mongod
Mysql / sqlplus mongo
3. 3
50177004-Big Data Analytics-Thinnaphat Borirak
การจัดเก็บข้อมูลแบบ JSON ใน MongoDB
1. เริมเอกสารด้วยการสร้างเครืองหมายปีกกาเปิด-ปิด { }
2. ข้อมูลแต่ละ Field คันด้วยเครืองหมายคอมม่า (,)
3. สามารถสร้างเอกสารย่อยในเอกสารหลักได้โดยการใส่ข้อมูลในเครืองหมายปีกกาเปิด-ปิด ตามตัวอย่างดังนี
{
_id:”1234567890t995a”,
name: ”Abusayard TK Limon”,
email: “abusa@example.com”,
course: {
course_name: “MongoDB”
}
}
*** ในฐานข้อมูล MongoDB จะทําการแปลงเอกสาร JSON โดยเข้ารหัสให้อยู่ในรูปแบบ Binary หรือทีเรียกว่า Binary
JSON: BSON เพือขยายความสามารถให้ JSON มีชนิดข้อมูลเพิมมากขึน มีการเข้ารหัส-ถอดรหัสทีด้วยภาษาที
แตกต่างกัน
Example: Shows the document structure of a blog site which is simply a comma separated key value pair.
{
_id: ObjectId(1ab23cd4567e)
title: “MongoDB Overview”,
description: “MongoDB is NoSQL Database”,
by: “Top”,
email: “thinnaphat.bo@gmail.com”,
tags: [“mongodb”, “database”, “NoSQL”],
likes: 100,
comments:
[
{
user:”adam”,
message: “Adam_first comment”,
dateCreated: new Date(2023,1,20,2,15),
like: 0
},
{
user:”Tony”,
message: “Tony_first comment”,
dateCreated: new Date(2023,1,21,2,15),
like: 3
}
]
}
NOTE
_id คือ เลขฐานสิบหกขนาด 12 ไบต์ซึงรับประกันความเป็นเอกลักษณ์ของเอกสารทุกฉบับ โดยสามารถระบุ _id ขณะ
ใส่เอกสารได้ หากไม่ได้ทําการระบุ โปรแกรมของ MongoDB จะให้รหัสเฉพาะสําหรับทุกเอกสารเอง ซึงจะมีทังหมด 12
4. 4
50177004-Big Data Analytics-Thinnaphat Borirak
ไบต์ - 4 ไบต์แรก สําหรับการระบุเวลาปัจจุบัน 3 ไบต์ถัดไปสําหรับรหัสเครือง 2 ไบต์ถัดไปสําหรับรหัสกระบวนการ
ของเซิร์ฟเวอร์ MongoDB และ 3 ไบต์ทีเหลือเป็นค่าทีเพิมขึนแบบ incremental Value
MongoDB Advantages
Any Relational Database has a typical schema design that shows number of tables and the relationship
between these tables. While in MongoDB there is no concept of relationship
Northwind Database
MongoDB Database
ข้อดีของ MongoDB Over RDBMS
Schema less: MongoDB เป็นฐานข้อมูลเอกสารทีคอลเลกชันหนึงเก็บเอกสารทีแตกต่างกัน จํานวนฟิลด์ เนือหา
และขนาดของเอกสารอาจแตกต่างกันไปในแต่ละเอกสาร
โครงสร้างของวัตถุชินเดียวมีความชัดเจน
ไม่มีการรวมทีซับซ้อน
ความสามารถในการค้นหาเชิงลึก MongoDB รองรับการสืบค้นแบบไดนามิกบนเอกสารโดยใช้ภาษาการสืบค้นตาม
เอกสาร ซึงเกือบจะมีประสิทธิภาพเทียบเท่ากับ SQL
การปรับแต่ง MongoDB ปรับขนาดได้ง่าย
ไม่จําเป็นต้อง Conversion หรือ mapping of application objects to database objects
ใช้หน่วยความจําภายในสําหรับจัดเก็บชุดการทํางาน ทําให้เข้าถึงข้อมูลได้เร็วขึน
5. 5
50177004-Big Data Analytics-Thinnaphat Borirak
Why should use MongoDB
Document Oriented Storage : Data is stored in the form of JSON style documents
Index on any attribute
Replication & High Availability
Auto-Sharing
Rich Queries
Fast In-Place Updates
Professional Support By MongoDB
Where should use MongoDB?
Big Data
Content Management and Delivery
Mobile and Social Infrastructure
User Data Management
Data Hub (ศูนย์กลางข้อมูลคือศูนย์กลางของการแลกเปลียนข้อมูลทีได้รับการสนับสนุนจากวิทยาศาสตร์ข้อมูล
วิศวกรรมข้อมูล และเทคโนโลยีคลังข้อมูล เพือโต้ตอบกับปลายทาง เช่น แอปพลิเคชันและอัลกอริทึม)
ตัวอย่างการนําข้อมูลไปใช้ในภาคอุตสาหกรรมรถยนต์
โดยทัวไปแล้วการจัดการกลุ่มยานพาหนะ ก็มีข้อมูลต่างๆทีเกียวข้องมากมาย ขึนอยู่กับวิธีทีบริษัทฯ นัน
จัดการและใช้ข้อมูล อาทิเช่น กระบวนการเพือรวบรวมข้อมูล กระบวนการในการนําข้อมูลไปวิเคราะห์ และใช้ประโยชน์
จากข้อมูลให้เกิดประโยชน์สูงสุด อย่าง Optimum Automotive ซึงได้พัฒนา DATA HUB ทีไม่เหมือนใครโดยใช้อัลกอริทึม
อัจฉริยะ (Smart Algorithms) ในการออกแบบและพัฒนาให้เกิดความก้าวหน้าทางด้านอุตสาหกรรมยานยนต์ดังนี
ทีมา: https://www.optimum-automotive.com/?-data-hub_fleet_managment-
Assignment: ยกตัวอย่างพร้อมเหตุผลประกอบในการนําข้อมูลมาใช้เพือให้เกิดประโยชน์
Assignment: คุณลองเข้าไปดูรายละเอียดของการใช้งานฐานข้อมูลในรูปแบบต่างๆ ในปีทีผ่านมา Most Popular
Databases In The World (2023) แล้วตอบคําถามว่าฐานข้อมูลทีบริษัทฯ หรือหน่วยงานทีสังกัดอยู่นัน ใช้ระบบ
ฐานข้อมูลใด เพราะเหตุใด มีแนวโน้มทีจะปรับเปลียนหรือไม่ อย่างไร
6. 6
50177004-Big Data Analytics-Thinnaphat Borirak
Workshop: การติดตัง MongoDB
MongoDB (https://www.mongodb.com/)
หากต้องการติดตัง MongoDB ก่อนอืนให้ดาวน์โหลด MongoDB รุ่นล่าสุด โดยสามารถเข้าไป Download โปรแกรมเพือ
ใช้ในการจัดการได้ที https://www.mongodb.com/try/download/community ซึงผมจะติดตังโปรแกรมบน
ระบบปฏิบัติการ Windows 64 bit และควรตรวจสอบให้แน่ใจว่า MongoDB เวอร์ชันทีใช้สามารถรันได้ผ่าน
ระบบปฏิบัติการของเราด้วยครับ
* สามารถพิมพ์คําสังทีใช้ในการตรวจสอบระบบปฏิบัติการได้ดังนี C:>wmic os get osarchitecture
Link Download Program:
https://drive.google.com/file/d/1AuSykawONILZ8e_2JzGYhgn3imDSoZXT/view?usp=sharing
สําหรับบุคคลทีต้องการติดตังบนระบบปฏิบัติการอืน อย่างเช่น Ubuntu ก็สามารถเข้าไปทีลิงค์นีได้
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
เครืองมือพืนฐานในการใช้งาน
- MongoDB Community เป็น MongoDB Server รุ่น Open Source
- MongoDB Compass เป็นเครืองมือในการจัดเก็บข้อมูลในรูปแบบ GUI ในการอํานวยความสะดวกในการวิเคราะห์
ข้อมูล การดําเนินการเก็บข้อมูล (CRUD) รวมทังสืบค้นและจัดการ Index เป็นต้น
9. 9
50177004-Big Data Analytics-Thinnaphat Borirak
What are the Documents in MongoDB
Document เป็นหน่วยพืนฐานของข้อมูลใน MongoDB ซึงโดยทัวไปเทียบเท่ากับแถวตามหลักการออกแบบ
แนวคิดของระบบ RDBMS เปรียบได้ว่า Document คือ หัวใจหลักของ MongoDB นันเอง โดย Document ในทีนีก็ คือ
ชุดคีย์ทีเรียงลําดับกันซึงมีข้อมูลหรือค่าทีเกียวข้องกับการแสดงข้อมูลของ Document เหล่านัน ไม่ว่าจะเป็น Map,
Hash หรือ Dictionary ในกรณีของ JavaScript เอกสารจะแสดงถึงข้อมูลทีเป็น Object เช่นเดียวกับรายละเอียดการใช้
งานทีสําคัญของ MongoDB
Data Types of MongoDB - นันสามารถรองรับประเภทข้อมูลทีหลากหลายได้ในค่าของ Document ซึง MongoDB มัก
ถูกมองว่าเป็นข้อมูลทีเหมือน JSON ทีเป็น JavaScript Object โดย JSON นันเป็นการแสดงข้อมูลอย่างง่าย ข้อดีของ
JSON คือ ช่วยให้เข้าใจในเรืองของการวิเคราะห์ในแต่ละเฟสและง่ายต่อการจํา นอกจากนี JSON ยังยอมรับค่าเฉพาะ
ข้อมูลทีเป็นประเภท null, Boolean, Number, String, Array และ Object ตามรายการด้านล่างคือประเภทข้อมูลทีมีอยู่
ใน MongoDB
String This is most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid.
Integer This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your
server.
Boolean This type is used to store a Boolean (true/ false) value.
Double This type is used to store floating point values.
Min/Max keys This type is used to compare a value against the lowest and highest BSON elements.
Arrays This type is used to store arrays or list or multiple values into one key.
Date This datatype is used to store the current date or time in UNIX time format. You can specify your
own datetime by creating object of Date and passing day, month, year into it.
Timestamp This can be handy for recording when a document has been modified or added.
Object This datatype is used for embedded documents.
Object ID This datatype is used to store the document’s ID.
Null This type is used to store a Null value.
Symbol This datatype is used identically to a string however, it's generally reserved for languages that use
a specific symbol type.
Binary data This datatype is used to store Binary data.
Code This datatype is used to store JavaScript code into document.
Regular expression This datatype is used to store regular expression
การสร้างแบบจําลองข้อมูล MongoDB
ให้คิดซะว่าข้อมูลใน MongoDB มี schema.documents ทียืดหยุ่นในคอลเล็กชันเดียวกันไม่จําเป็นต้องมีชุดฟิลด์หรือ
โครงสร้างชุดเดียวกัน และฟิลด์ทัวไปในเอกสารของคอลเล็กชันอาจมีข้อมูลประเภทต่างๆ กัน
ข้อควรพิจารณาบางประการขณะออกแบบสคีมา (Designing Schema) ใน MongoDB
ออกแบบสคีมาตามความต้องการของผู้ใช้
รวม Object เป็นเอกสารเดียวกันถ้าหากต้องการใช้ร่วมกัน แต่ถ้าไม่ควรให้แยกออกจากกัน
ทําสําเนาข้อมูลให้สมําเสมอ ให้คิดว่าพืนทีในการจัดเก็บเทียบกับเวลาทีใช้ในการคํานวณไม่ได้
ปรับสคีมาให้เหมาะสมสําหรับกรณีการใช้งานส่วนทีใช้บ่อยทีสุด
11. 11
50177004-Big Data Analytics-Thinnaphat Borirak
Workshop: Create Database
The use Command
MongoDB use DATABASE_NAME is used to create database. The command will create a new database, if it
doesn't exist otherwise it will return the existing database.
Syntax:
Basic syntax of use DATABASE statement is as follows:
use DATABASE_NAME
Example:
If you want to create a database with name <mydb>, then use DATABASE statement would be as follows:
>use mydb
switched to db mydb
To check your currently selected database use the command db
>db
mydb
If you want to check your databases list, then use the command show dbs.
>show dbs
admin 40.00 KiB
config 60.00 KiB
local 72.00 KiB
Your created database (mydb) is not present in list. To display database you need to insert atleast one
document into it.
>db.movie.insert({"name":"Avatar"})
>show dbs
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
mydb 40.00 KiB
* In mongodb default database is test. If you didn't create any database then collections will be stored in test
database.
Example: Try writing a command to add two more movie titles.
>
>
12. 12
50177004-Big Data Analytics-Thinnaphat Borirak
Workshop: Drop Database
The dropDatabase () Method
MongoDB db.dropDatabase () command is used to drop a existing database.
Syntax:
Basic syn tax of dropDatabase () command is as follows:
db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will delete default 'test'
database
Example:
First, check the list available databases by using the command show dbs
>show dbs
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
mydb 40.00 KiB
If you want to delete new database <mydb>, then dropDatabase() command would be as follows:
>use mydb
switched to db mydb
>db.dropDatabase()
{ ok: 1, dropped: 'mydb' }
Now check list of databases
>show dbs
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
13. 13
50177004-Big Data Analytics-Thinnaphat Borirak
Workshop: Create Collection
The createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.
Syntax:
Basic syntax of createCollection() command is as follows
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and used to specify
configuration of collection
Parameter Type Description
Name String Name of the collection to be created
Options Document (Optional) Specify options about memory size and indexing
Options parameter is optional, so you need to specify only name of the collection. Following is the list of options
you can use:
Field Type Description
capped Boolean (Optional) If true, enables a capped collection. Capped collection is a collection
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.
autoIndexId Boolean (Optional) If true, automatically create index on _id field.s Default value is false.
size number (Optional) Specifies a maximum size in bytes for a capped collection. If capped is
true, then you need to specify this field also.
max number (Optional) Specifies the maximum number of documents allowed in the capped
collection.
Examples:
Basic syntax of createCollection() method without options is as follows
>use mydb
'switched to db mydb'
>db.createCollection("mycollection")
{ ok: 1 }
You can check the created collection by using the command show collections
>show collections
mycollection
14. 14
50177004-Big Data Analytics-Thinnaphat Borirak
Following example shows the syntax of createCollection() method with few important options:
>db.createCollection("mycollection_option",{capped:true,autoIndexId:true,size:6142800,max:10000})
{ ok: 1 }
In MongoDB you don't need to create collection. MongoDB creates collection automatically, when you insert
some document.
>db.mycollection_50177004.insert({"name":"Big Data Analytics"})
>show collections
mycollection
mycollection_50177004
mycollection_option
Note: Rename Collection >db.collection_name.renameCollection("collection_New_name")
Workshop: Drop Collection
The drop() Method
MongoDB's db.collection.drop() is used to drop a collection from the database.
Syntax:
Basic syntax of drop() command is as follows
db.COLLECTION_NAME.drop()
Example:
First, check the available collections into your database mydb
> use mydb
'already on db mydb'
>show collections
mycollection
mycollection_50177004
mycollection_option
Now drop the collection with the name mycollection
> db.mycollection.drop()
true
drop() method will return true, if the selected collection is dropped successfully otherwise it will return false
Again check the list of collections into database
> show collections
mycollection_50177004
15. 15
50177004-Big Data Analytics-Thinnaphat Borirak
mycollection_option
Example: drop the collection with the name mycollection_50177004 and mycollection_option
>
>
Now check the list of collections into database
>
Now check list of databases
>
Why is there no database named mydb?
Workshop: Insert Document
The insert() Method
To insert data into MongoDB collection, you need to use MongoDB's insert() or save()method.
Syntax
Basic syntax of insert() command is as follows:
>db.COLLECTION_NAME.insert(document)
Example
>db.mycollection.insert({
title: 'MongoDB Insert() Method',
description: 'MongoDB is NoSQL Database',
by: 'Abusayard TK Limon',
url: 'https://www.mongodb.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
Example
>db.post.insert( [
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Abusayard TK Limon',
url: 'https://www.mongodb.com',
16. 16
50177004-Big Data Analytics-Thinnaphat Borirak
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: 'NoSQL database does not have tables',
by: 'Thinnaphat',
email: 'thinnaphat.bo@gmail.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 30,
comments: [
{
user:'Adam',
message: 'Adam first comment',
dateCreated: new Date(2023,01,28,2,35),
like: 0
} ]
}
] )
Note: InsertOne - db.Collection_name.insertOne( <Document> )
InsertMany - db.Collection_name.insertMany( [ <Document> ] )
Workshop: Basic Query Document
The find() Method
To query data from MongoDB collection, you need to use MongoDB's find() method.
Syntax
Basic syntax of find() method is as follows
>db.COLLECTION_NAME.find()
find() method will display all the documents in a non structured way.
Example
>db.post.find()
>db.post.find({"title":"MongoDB Overview"})
The pretty() Method
To display the results in a formatted way, you can use pretty() method.
Syntax:
>db.mycolection.find().pretty()